Top Banner
Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Syntaxanalyse
22

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

Mar 28, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Helmut Schauer Educational Engineering Lab Department for Information Technology University of Zurich Syntaxanalyse.

1

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Syntaxanalyse

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

2

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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 ")" ).

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

3

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Beispiel:Syntaxdiagramme für arithmetische Ausdrücke

exp: term:+

term

-

*

factor

/

factor:

exp

var

const

)(

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

4

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Beispiel: (x+1)*3

exp

exp

term

term

const

factor factor

+

)(

*

term 3

const

factor

1

var

factor

x

Parsebaum Codebaum

*

+ 3

1x

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

5

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Beispiel:Syntax für Lexical Scan

var = letter { letter | digit }.const = digit { digit }.letter = "a"|"b"| ... |"z".digit = "0"|"1"| ... |"9".

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

6

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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; }}

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

7

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Interpreter

Interpreter x=2 5

ParameterWert

Ergebnis

Wert

Code Baum

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

8

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Compiler

Compiler (x+1)*3

Syntax

Code

Baum

Programm

Text

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

9

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Compiler und Interpreter

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

10

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Generator

Generator exp = term {("+"|"-") term}.term = factor {("*"|"/") factor}.factor = var | const | ( "(" exp ")" ).

MetaSyntax

SyntaxEBNF

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

11

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Generator

Generator

MetaSyntax

EBNF

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

12

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Generator, Compiler und Interpreter

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

13

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Automaten (1)

Beispiel: Berechnung des Wertes einer Dezimalzahl mittels eines Automaten

EBNF: number = digit {digit} ["."{digit}]Syntaxdiagramm:

Übergangsdiagramm:

0 321start stopdigit

digit digit

"."

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

14

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Automaten (2)

Übergangsdiagramm:

Zustandstabelle s: Aktionstabelle a:

0 321start stopdigit

digit digit

"."

digit "." else

0 1 error error

1 1 2 3

2 2 error 3

digit "." else

0 1 5 5

1 2 0 4

2 3 5 4

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

15

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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");}

}

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

16

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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]);

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

17

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Shift-Reduce Syntaxanalyse (1)

Operator-Precedence: Precedence-Funktionen:

x < y f(x) < g(y)x > y f(x) > g(y)

f g

var 4 5

+ 2 1

* 4 3

empty 0 0

var + * empty

var > > >

+ < > < >

* < > > >

empty < < <

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

18

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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)) { // shifts.push(token);token = lexer.nextToken();} else { // reducedo {op = s.pop(); action(op);}while (f(s.top())<=g(token));}}

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

19

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity of Zurich

Shift-Reduce Syntaxanalyse (3)Beispiel: text = "a+b*c"

a +b+

*+

c*+

*+ +

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

20

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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; }

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

21

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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; }

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

22

Helmut SchauerEducational Engineering LabDepartment for Information TechnologyUniversity 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; }}