• Keine Ergebnisse gefunden

Parser Generators

N/A
N/A
Protected

Academic year: 2022

Aktie "Parser Generators"

Copied!
44
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Compilers and Language Processing Tools

Summer Term 2011

Prof. Dr. Arnd Poetzsch-Heffter

Software Technology Group TU Kaiserslautern

c

Prof. Dr. Arnd Poetzsch-Heffter 1

(2)

Parser Generators

Parser Generators

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 2

(3)

Parser Generators

Preparations

$ mkdir clp11

$ cd clp11

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 3

(4)

Parser Generators

ANTLR

ANTLR

Parser generator for LL(k) grammars (LL(*))

Integrates Scanner generator and AST generator

$ java -jar /home/j_schaef/public/antlrworks-1.4.2.jar

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 4

(5)

Parser Generators

ANTLR

ANTLR

Parser generator for LL(k) grammars (LL(*))

Integrates Scanner generator and AST generator

$ java -jar /home/j_schaef/public/antlrworks-1.4.2.jar

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 5

(6)

Parser Generators

Simple Arithmetic Expressions

Simple arithmetic expressions: 5+4+33

Enter the following grammar into ANTLR:

grammar test;

expr : expr ’+’ expr

| NUM;

NUM : (’0’..’9’)*;

ANTLR Error: Rule ’expr’ is left-recursive

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 6

(7)

Parser Generators

Simple Arithmetic Expressions

Simple arithmetic expressions: 5+4+33 Enter the following grammar into ANTLR:

grammar test;

expr : expr ’+’ expr

| NUM;

NUM : (’0’..’9’)*;

ANTLR Error: Rule ’expr’ is left-recursive

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 7

(8)

Parser Generators

Simple Arithmetic Expressions

Simple arithmetic expressions: 5+4+33 Enter the following grammar into ANTLR:

grammar test;

expr : expr ’+’ expr

| NUM;

NUM : (’0’..’9’)*;

ANTLR Error: Rule ’expr’ is left-recursive

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 8

(9)

Parser Generators

Remove Left-Recursion

grammar test; expr : NUM exprp

| NUM; exprp : ’+’ expr; NUM : (’0’..’9’)*;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 9

(10)

Parser Generators

Remove Left-Recursion

grammar test;

expr : NUM exprp

| NUM;

exprp : ’+’ expr;

NUM : (’0’..’9’)*;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 10

(11)

Parser Generators

Java CUP

Java CUP

Parser generator for LALR grammars

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 11

(12)

Parser Generators

Simple Expressions

expr ::= expr ’+’ expr

| number

number ::= 0

| [1-9][0-9]+

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 12

(13)

Parser Generators

Steps

1. Identify and name the terminal and non-terminal symbols 2. Define a grammar

3. Define an unambiguous grammar

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 13

(14)

Parser Generators

Steps

1. Identify and name the terminal and non-terminal symbols

2. Define a grammar

3. Define an unambiguous grammar

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 14

(15)

Parser Generators

Steps

1. Identify and name the terminal and non-terminal symbols 2. Define a grammar

3. Define an unambiguous grammar

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 15

(16)

Parser Generators

Steps

1. Identify and name the terminal and non-terminal symbols 2. Define a grammar

3. Define an unambiguous grammar

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 16

(17)

Parser Generators

First Try

Parser.cup:

terminal PLUS, NUMBER;

non terminal expr;

expr ::= expr PLUS expr

| NUMBER

;

$ java -jar java-cup.jar Parser.cup

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 17

(18)

Parser Generators

First Try

Parser.cup:

terminal PLUS, NUMBER;

non terminal expr;

expr ::= expr PLUS expr

| NUMBER

;

$ java -jar java-cup.jar Parser.cup

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 18

(19)

Parser Generators

Java CUP Output

Warning : *** Shift/Reduce conflict found in state #5 between expr ::= expr PLUS expr (*)

and expr ::= expr (*) PLUS expr under symbol PLUS

Resolved in favor of shifting.

Error : *** More conflicts encountered than expected -- parser generation aborted

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 19

(20)

Parser Generators

Debugging the Generated Parser

Use the-dump_statesoption of JavaCUP.

$ java -jar java-cup.jar -dump_states Parser.cup

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 20

(21)

Parser Generators

===== Viable Prefix Recognizer =====

START lalr_state [0]: {

[expr ::= (*) NUMBER , {EOF PLUS }]

[$START ::= (*) expr EOF , {EOF }]

[expr ::= (*) expr PLUS expr , {EOF PLUS }]

}

transition on expr to state [2]

transition on NUMBER to state [1]

--- lalr_state [1]: {

[expr ::= NUMBER (*) , {EOF PLUS }]

}

--- lalr_state [2]: {

[$START ::= expr (*) EOF , {EOF }]

[expr ::= expr (*) PLUS expr , {EOF PLUS }]

}

transition on EOF to state [4]

transition on PLUS to state [3]

--- lalr_state [3]: {

[expr ::= (*) NUMBER , {EOF PLUS }]

[expr ::= expr PLUS (*) expr , {EOF PLUS }]

[expr ::= (*) expr PLUS expr , {EOF PLUS }]

}

transition on expr to state [5]

transition on NUMBER to state [1]

--- lalr_state [4]: {

[$START ::= expr EOF (*) , {EOF }]

}

--- lalr_state [5]: {

[expr ::= expr PLUS expr (*) , {EOF PLUS }]

[expr ::= expr (*) PLUS expr , {EOF PLUS }]

}

transition on PLUS to state [3]

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 21

(22)

Parser Generators

LALR(1) Item Automata

expr.NUMBER EOF, PLUS

start.expr EOF EOF

expr.expr PLUS expr EOF, PLUS s0

exprNUMBER. EOF, PLUS

s1

startexpr.EOF EOF

exprexpr.PLUS expr EOF, PLUS s2

expr.NUMBER EOF, PLUS

exprexpr PLUS.expr EOF, PLUS expr.expr PLUS expr EOF, PLUS s3

startexpr EOF. EOF

s4

exprexpr PLUS expr. EOF, PLUS exprexpr.PLUS expr EOF, PLUS s5

NUMBER

expr

EOF

PLUS

NUMBER

expr PLUS

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 22

(23)

Parser Generators

Unambiguous Grammar

expr ::= expr PLUS expr

| NUMBER

;

Unambiguous Grammar?

GrammarΓ3from lecture (p.71):

expr ::= expr PLUS num

| num

;

num ::= NUMBER;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 23

(24)

Parser Generators

Unambiguous Grammar

expr ::= expr PLUS expr

| NUMBER

;

Unambiguous Grammar?

GrammarΓ3from lecture (p.71):

expr ::= expr PLUS num

| num

;

num ::= NUMBER;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 24

(25)

Parser Generators

JavaCUP States

===== Viable Prefix Recognizer =====

START lalr_state [0]: { [expr ::= (*) num , {EOF PLUS }]

[expr ::= (*) expr PLUS num , {EOF PLUS }]

[num ::= (*) NUMBER , {EOF PLUS }]

[$START ::= (*) expr EOF , {EOF }]

}

transition on expr to state [3]

transition on NUMBER to state [2]

transition on num to state [1]

--- lalr_state [1]: {

[expr ::= num (*) , {EOF PLUS }]

}

--- lalr_state [2]: {

[num ::= NUMBER (*) , {EOF PLUS }]

}

--- lalr_state [3]: {

[expr ::= expr (*) PLUS num , {EOF PLUS }]

[$START ::= expr (*) EOF , {EOF }]

}

transition on EOF to state [5]

transition on PLUS to state [4]

--- lalr_state [4]: {

[expr ::= expr PLUS (*) num , {EOF PLUS }]

[num ::= (*) NUMBER , {EOF PLUS }]

}

transition on NUMBER to state [2]

transition on num to state [6]

--- lalr_state [5]: {

[$START ::= expr EOF (*) , {EOF }]

}

--- lalr_state [6]: {

[expr ::= expr PLUS num (*) , {EOF PLUS }]

}

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 25

(26)

Parser Generators

LALR(1) Item Automata

expr.num EOF, PLUS

expr.expr PLUS expr EOF, PLUS

num.NUMBER EOF, PLUS

start.expr EOF EOF

s0

exprnum. EOF, PLUS

s1

numNUMBER. EOF, PLUS

s2

exprexpr.PLUS expr EOF, PLUS

startexpr.EOF EOF

s3

exprexpr PLUS.expr EOF, PLUS

num.NUMBER EOF, PLUS

s4

startexpr EOF. EOF

s5

exprexpr PLUS num. EOF, PLUS s6

num

NUMBER

expr

PLUS EOF

NUMBER

num

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 26

(27)

Parser Generators

Action and Reduce Tables

-dump_tables -dump_grammar --- ACTION_TABLE --- From state #0

[term 3:SHIFT(to state 2)]

From state #1

[term 0:REDUCE(with prod 2)]

[term 2:REDUCE(with prod 2)]

From state #2

[term 0:REDUCE(with prod 3)]

[term 2:REDUCE(with prod 3)]

From state #3

[term 0:SHIFT(to state 5)]

[term 2:SHIFT(to state 4)]

From state #4

[term 3:SHIFT(to state 2)]

From state #5

[term 0:REDUCE(with prod 0)]

From state #6

[term 0:REDUCE(with prod 1)]

[term 2:REDUCE(with prod 1)]

---

--- REDUCE_TABLE --- From state #0

[non term 0->state 3]

[non term 1->state 1]

From state #1 From state #2 From state #3 From state #4

[non term 1->state 6]

From state #5 From state #6 Terminals:

[0]EOF [1]error [2]PLUS [3]NUMBER Non terminals:

[0]expr [1]num Productions:

[0] $START ::= expr EOF [1] expr ::= expr PLUS num [2] expr ::= num

[3] num ::= NUMBER c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 27

(28)

Parser Generators

Example

1+2+3 => (Scanner) => NUMBER PLUS NUMBER PLUS NUMBER EOF

= NPNPNE

State Stack Input Rest Action

#0 #0 NPNPNE shift and goto #2

#2 #0 N#2 PNPNE reduce and goto #1

#1 #0 num#1 PNPNE reduce and goto #3

#3 #0 expr#3 PNPNE shift and goto #4

#4 #0 expr#3 P#4 NPNE shift and goto #2

#2 #0 expr#3 P#4 N#2 PNE reduce and goto #6

#6 #0 expr#3 P#4 num#6 PNE reduce and goto #3

#3 #0 expr#3 PNE shift and goto #4

#4 #0 expr#3 P#4 NE shift and goto #2

#2 #0 expr#3 P#4 N#2 E reduce and goto #6

#6 #0 expr#3 P#4 num#6 E reduce and goto #3

#3 #0 expr#3 E shift and goto #5

#5 #0 expr#3 E#5 reduce and goto #3

#3 #0 start#3

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 28

(29)

Parser Generators

Associativities

4 + 5 + 6 = (4 + 5 ) + 6

JavaCUP: Associativities on terminal symbols:

left, right, nonassoc

Example:

precedence left PLUS;

expr ::= expr PLUS expr

| NUMBER

;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 29

(30)

Parser Generators

Associativities

4 + 5 + 6 = (4 + 5 ) + 6

JavaCUP: Associativities on terminal symbols:

left, right, nonassoc

Example:

precedence left PLUS;

expr ::= expr PLUS expr

| NUMBER

;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 30

(31)

Parser Generators

Associativities

4 + 5 + 6 = (4 + 5 ) + 6

JavaCUP: Associativities on terminal symbols:

left, right, nonassoc

Example:

precedence left PLUS;

expr ::= expr PLUS expr

| NUMBER

;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 31

(32)

Parser Generators

Precedences

5 + 6 * 3 = 5 + (6 * 3)

JavaCUP: Precedence on terminal symbols:

same line = equal precedences,

farther down = higher precedence

Example:

precedence left PLUS, MINUS; // PLUS same as MINUS

precedence left MULT, DIV; // higher than PLUS and MINUS

expr ::= expr infixop expr

| NUMBER ;

infixop ::= PLUS | MINUS | MULT | DIV;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 32

(33)

Parser Generators

Precedences

5 + 6 * 3 = 5 + (6 * 3)

JavaCUP: Precedence on terminal symbols:

same line = equal precedences,

farther down = higher precedence

Example:

precedence left PLUS, MINUS; // PLUS same as MINUS

precedence left MULT, DIV; // higher than PLUS and MINUS

expr ::= expr infixop expr

| NUMBER ;

infixop ::= PLUS | MINUS | MULT | DIV;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 33

(34)

Parser Generators

Precedences

5 + 6 * 3 = 5 + (6 * 3)

JavaCUP: Precedence on terminal symbols:

same line = equal precedences,

farther down = higher precedence

Example:

precedence left PLUS, MINUS; // PLUS same as MINUS

precedence left MULT, DIV; // higher than PLUS and MINUS

expr ::= expr infixop expr

| NUMBER ;

infixop ::= PLUS | MINUS | MULT | DIV;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 34

(35)

Parser Generators

Rule Precedences

Rules have the precedence of their last terminal symbol

5 - - 4?

expr ::= expr MINUS expr

| MINUS expr

| NUMBER ;

Solution: Introduce "dummy" terminal symbol UMINUS:

precedence left MINUS; precedence left UMINUS; expr ::= expr MINUS expr

| MINUS expr %prec UMINUS

| NUMBER ;

Note: UMINUS is never returned by the scanner!

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 35

(36)

Parser Generators

Rule Precedences

Rules have the precedence of their last terminal symbol 5 - - 4?

expr ::= expr MINUS expr

| MINUS expr

| NUMBER ;

Solution: Introduce "dummy" terminal symbol UMINUS:

precedence left MINUS; precedence left UMINUS; expr ::= expr MINUS expr

| MINUS expr %prec UMINUS

| NUMBER ;

Note: UMINUS is never returned by the scanner!

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 36

(37)

Parser Generators

Rule Precedences

Rules have the precedence of their last terminal symbol 5 - - 4?

expr ::= expr MINUS expr

| MINUS expr

| NUMBER ;

Solution: Introduce "dummy" terminal symbol UMINUS:

precedence left MINUS; precedence left UMINUS; expr ::= expr MINUS expr

| MINUS expr %prec UMINUS

| NUMBER ;

Note: UMINUS is never returned by the scanner!

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 37

(38)

Parser Generators

Rule Precedences

Rules have the precedence of their last terminal symbol 5 - - 4?

expr ::= expr MINUS expr

| MINUS expr

| NUMBER ;

Solution: Introduce "dummy" terminal symbol UMINUS:

precedence left MINUS;

precedence left UMINUS;

expr ::= expr MINUS expr

| MINUS expr %prec UMINUS

| NUMBER ;

Note: UMINUS is never returned by the scanner!

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 38

(39)

Parser Generators

Semantic versus Syntax

Arithmetic and Boolean expressions For example:

a := 5 b := 4+a c := 3 = 3 d := 2+1 = 3 & c

Grammar:

terminal ID, EQUAL, PLUS, ASSIGN, AND; non terminal ae, be, stm;

precedence left AND; precedence left PLUS;

stm ::= ID ASSIGN ae | ID ASSIGN be; be ::= ae EQUAL ae | be AND be | ID; ae ::= ae PLUS ae | ID;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 39

(40)

Parser Generators

Semantic versus Syntax

Arithmetic and Boolean expressions For example:

a := 5 b := 4+a c := 3 = 3 d := 2+1 = 3 & c

Grammar:

terminal ID, EQUAL, PLUS, ASSIGN, AND;

non terminal ae, be, stm;

precedence left AND;

precedence left PLUS;

stm ::= ID ASSIGN ae | ID ASSIGN be;

be ::= ae EQUAL ae | be AND be | ID;

ae ::= ae PLUS ae | ID;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 40

(41)

Parser Generators

Handle Semantics Later

terminal ID, EQUAL, PLUS, ASSIGN, AND;

non terminal e, stm;

precedence left AND;

precedence left PLUS;

precedence left EQUAL;

stm ::= ID ASSIGN e;

e ::= ID

| e EQUAL e

| e AND e

| e PLUS e;

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 41

(42)

Parser Generators

Error Recovery

Problem

Typically JavaCUP stops when it encounters an error

I Only thefirsterror is shown

Solution

Local error recovery

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 42

(43)

Parser Generators

Example

expr ::= expr PLUS expr | ID;

exprlist ::= expr SEMI expr;

We want to be able to skip erroneous expressions in a list.

Introduce error production with special non-terminalerrorsymbol.

exprlist ::= error SEMI expr;

Important:theerrorsymbol should always be followed by a terminal synchronization symbol,SEMIin this example.

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 43

(44)

Parser Generators

Example

expr ::= expr PLUS expr | ID;

exprlist ::= expr SEMI expr;

We want to be able to skip erroneous expressions in a list.

Introduce error production with special non-terminalerrorsymbol.

exprlist ::= error SEMI expr;

Important:theerrorsymbol should always be followed by a terminal synchronization symbol,SEMIin this example.

c

Prof. Dr. Arnd Poetzsch-Heffter Practical Exercise 44

Referenzen

ÄHNLICHE DOKUMENTE

Von hier aus können wir jede der verschiedenen sym- bolischen Formen von der idealen Beziehung her definieren (und in gewissem Sinne logisch deduzieren), die sie

Symbolika ognia nie wiąże się wyłącznie z przedstawieniem kuźni, lecz raczej z osobą Wulkana, co zresztą zgodne jest z tekstami mitografów, którzy zawsze

Balle ich die Wolken über der Erde zusammen und erscheint der Bogen in den Wolken, dann gedenke ich des Bundes, der be- steht zwischen mir und euch und allen Lebewesen, allen Wesen

Feuer als Symbol für den Zorn Gottes Vor allem im Alten Testament lesen wir immer wieder auch vom zornigen und strafenden Gott.. Feuer als Ort

Diese Zeichen können aber auch ein Symbol für unser Leben sein. Unser Leben führt uns nicht immer

Empfi nde noch einmal das Kreuz in deinem Körper nach: die Beine, die fest auf der Erde stehen, dein Kopf, der zum Himmel ragt, deine beiden Arme, die nach rechts und links

[r]

where cols includes one character [lrc] for each column (with optional characters | inserted for vertical lines) and row j includes character & a total of (n − 1) times to