1 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Syntaxanalyse.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

LEUCEMIA MIELOIDE AGUDA TIPO 0
You have been given a mission and a code. Use the code to complete the mission and you will save the world from obliteration…
JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Slide 1 Insert your own content. Slide 2 Insert your own content.
Chapter 6 Writing a Program
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 5 Author: Julia Richards and R. Scott Hawley.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Source of slides: Introduction to Automata Theory, Languages and Computation.
Chapters 1 & 2 Theorem & Postulate Review Answers
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ZMQS ZMQS
1 2.Lexical Analysis 2.1Tasks of a Scanner 2.2Regular Grammars and Finite Automata 2.3Scanner Implementation.
Chapter 2-2 A Simple One-Pass Compiler
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
ABC Technology Project
1 / / / /. 2 (Object) (Object) –, 10 (Class) (Class) –, –, – (Variables) [ Data member Field Attribute](, ) – (Function) [ Member function Method Operation.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
O X Click on Number next to person for a question.
© S Haughton more than 3?
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
© Charles van Marrewijk, An Introduction to Geographical Economics Brakman, Garretsen, and Van Marrewijk.
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
Take from Ten First Subtraction Strategy -9 Click on a number below to go directly to that type of subtraction problems
Linking Verb? Action Verb or. Question 1 Define the term: action verb.
Squares and Square Root WALK. Solve each problem REVIEW:
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
Chapter 5 Test Review Sections 5-1 through 5-4.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
We will resume in: 25 Minutes.
Pointers and Arrays Chapter 12
1 Ke – Kitchen Elements Newport Ave. – Lot 13 Bethesda, MD.
A SMALL TRUTH TO MAKE LIFE 100%
1 Unit 1 Kinematics Chapter 1 Day
O X Click on Number next to person for a question.
How Cells Obtain Energy from Food
1 Assignment 3 Jianguo Lu. 2 Task: check whether the a program is syntactically correct /** this is a comment line in the sample program **/ INT f2(INT.
Chapter 8 Improving the User Interface
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Introduction To Compilers And Phase 1 Inside a compiler. Inside a C-- compiler. The compilation process. Example C-- code. Extended Backus-Naur.
Grammars Examples and Issues. Examples from Last Lecture a + b a b + a*bc* First draw a state diagram Then create a rule for each transition.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
CS 363 Comparative Programming Languages
Introduction to Java Programming
Presentation transcript:

1 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Syntaxanalyse

2 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Beispiel: Syntax für arithmetische Ausdrücke in Erweiterter Backus-Naur Form (EBNF) exp = term {("+"|"-") term}. term = factor {("*"|"/") factor}. factor = var | const | ( "(" exp ")" ).

3 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Beispiel: Syntaxdiagramme für arithmetische Ausdrücke exp:term: + term - * factor / factor: exp var const )(

4 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Beispiel: (x+1)*3 exp term const factor + )( * term3 const factor 1 var factor x Parsebaum Codebaum * x

5 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Beispiel: Syntax für Lexical Scan var = letter { letter | digit }. const = digit { digit }. letter = "a"|"b"|... |"z". digit = "0"|"1"|... |"9".

6 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Beispiel: Lexical Scan public class LexicalScanner extends StringTokenizer { public LexicalScanner(String string) { super(string.trim(),"+-*/^=?()' \n",true); } public String nextToken() { // return words, numbers, operators, brackets or empty string String token; do { if (hasMoreElements()) token = super.nextToken().trim(); else return ""; // return empty string for end of text } while (token.equals("")||token.equals("\n")); // skip spaces and \n return token; }

7 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Interpreter x=2 5 Parameter Wert Ergebnis Wert Code Baum

8 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Compiler (x+1)*3 Syntax Code Baum Programm Text

9 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Compiler und Interpreter

10 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Generator exp = term {("+"|"-") term}. term = factor {("*"|"/") factor}. factor = var | const | ( "(" exp ")" ). MetaSyntax Syntax EBNF

11 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Generator MetaSyntax EBNF

12 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Generator, Compiler und Interpreter

13 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Automaten (1) Beispiel: Berechnung des Wertes einer Dezimalzahl mittels eines Automaten EBNF: number = digit {digit} ["."{digit}] Syntaxdiagramm: Übergangsdiagramm: start stop digit "."

14 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Automaten (2) Übergangsdiagramm: Zustandstabelle s: Aktionstabelle a: start stop digit "." digit"."else 01error digit"."else

15 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Automaten (3) void action(int i) throws Exception { switch (i) { case 1: w = digit; n = 1; break; case 2: w = 10*w+digit; break; case 3: w = 10*w+digit; n = 10*n; break; case 4: result = w/n; break; case 5: throw new Exception( "bad number" ); }

16 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Automaten (4) final int start = 0; final int stop = 3; final int error = 4; int state = start; int symbol; do { symbol = nextSymbol(); action(a[state][symbol]); state = s[state][symbol]; } while (state < stop); action(a[state][symbol]);

17 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Shift-Reduce Syntaxanalyse (1) Operator-Precedence: Precedence-Funktionen: x < y f(x) < g(y) x > y f(x) > g(y) fg var *43 empty00 var+*empty var > > > + <><> * <> > > empty < < <

18 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Shift-Reduce Syntaxanalyse (2) final String empty = "" ; LexicalScanner lexer = new LexicalScanner(text); String token = lexer.nextToken(); Stack s = new Stack(); s.push(empty); while (!(s.top().equals(empty)&token.equals(empty))) { if (f(s.top())<=g(token)) { // shift s.push(token); token = lexer.nextToken(); } else { // reduce do {op = s.pop(); action(op);} while (f(s.top())<=g(token)); }

19 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Shift-Reduce Syntaxanalyse (3) Beispiel: text = "a+b*c" a+ b+b+ *+*+ c*+c*+ *+*+ +

20 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich InfixAnalyzer (1) class InfixAnalyzer { // recursive descent syntax analysis: public Operand exp() throws Warning { Operand op = term(); while (true) { if (nextTokenEquals("-")) op = Operand.genDiff(op,term()); else if (nextTokenEquals("+")) op = Operand.genSum(op,term()); else break; } return op; }

21 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich InfixAnalyzer (2) public Operand term() throws Warning { Operand op = factor(); while (true) { if (nextTokenEquals("/")) op = Operand.genQuot(op,factor()); else if (nextTokenEquals("*")) op = Operand.genProd(op,factor()); else break; } return op; }

22 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich InfixAnalyzer (3) public Operand factor() throws Warning { Operand op = Operand.UNDEF; if (token.equals("")) throw new Warning("factor is missing!"); if (isNumber()) { // factor = constant try {op = Operand.genConst(Integer.parseInt(token));} catch(NumberFormatException e) { throw new Warning(token+" is not a number!"); } token = lexer.nextToken(); } else if (isVariable()) { // factor = variable op = Operand.genVar(token); token = lexer.nextToken(); } else if (nextTokenEquals("(")) { // factor = ( exp ) op = exp(); if (!nextTokenEquals(")")) throw new Warning(") is missing!"); } else throw new Warning("factor is missing!"); return op; }