CSE P501 – Compilers. Parsing Context Free Grammars (CFG) Ambiguous Grammars Next. Parsing. ‘Middle End’. Back End. Target. Source. Front End. chars. IR. IR. Scan. Select Instructions. Optimize. tokens. IR. Allocate Registers. Parse. IR. AST. Emit. Convert. IR. IR.
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Parsing
Context Free Grammars (CFG)
Ambiguous Grammars
Next
Jim Hogg  UW  CSE  P501
‘Middle End’
Back End
Target
Source
Front End
chars
IR
IR
Scan
Select Instructions
Optimize
tokens
IR
Allocate Registers
Parse
IR
AST
Emit
Convert
IR
IR
Machine Code
AST = Abstract Syntax Tree
IR = Intermediate Representation
Jim Hogg  UW  CSE  P501
MiniJava includes the following tokens (among many others):
So a MiniJavaScanner would happily accept the following program:
We rely on a MiniJavaParser to reject this kind of gibberish
But how do we specify what makes a validMiniJava program?
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Legal
Hokum BNF Grammar
a= 1; b = a + 4;
z = 1;
if b + 3 then z = 2
ProgStm;ProgStm
StmAsStmIfStm
AsStm Var=Exp
IfStm ifExpthenAsStm
VorC VarConst
Exp VorCVorC+VorC
Var [az]
Const [09]
Illegal
a= x < 20
b= a + 4 + 5 ;
z = 1
if (a == 33) z < 2 ;
But how do we know which programs are legal or illegal, in Hokum?
Jim Hogg  UW  CSE  P501
StmAsStmIfStm
AsStm Var=Exp
IfStm ifExpthenAsStm
VorC VarConst
Exp VorCVorC+VorC
Var [az]
Const [09]
DerivationProg
=> Stm; Prog
=> AsStm; Prog
=> Var= Exp ; Prog
=> a = Exp; Prog
=> a = VorC ; Prog
=> a = Const ; Prog
=> a = 1 ; Prog
=> a = 1 ; Stm
=> a = 1 ; IfStm
=> a = 1 ; if Expthen AsStm
=> a = 1 ; if VorC+ VorC then AsStm
=> a = 1 ; if Var + VorC then AsStm
=> a = 1 ; if a + VorCthen AsStm
=> a = 1 ; if a + Const then AsStm
=> a = 1 ; if a + 1 then AsStm
=> a = 1 ; if a + 1 then Var = Exp
=> a = 1 ; if a + 1 then b = Exp
=> a = 1 ; if a + 1 then b = VorC
=> a = 1 ; if a + 1 then b = Const
=> a = 1 ; if a + 1 then b = 2
Jim Hogg  UW  CSE  P501
StmAsStmIfStm
AsStm Var=Exp
IfStm ifExpthenAsStm
VorC VarConst
Exp VorCVorC+VorC
Var [az]
Const [09]
Parse TreeProg
Prog
;
Stm
Stm
AsStm
IfStm
Var
=
Exp
then
Exp
if
AsStm
a
VorC
Var
=
Exp
VorC
+
VorC
Const
VorC
b
Var
Const
Const
1
1
a
2
Jim Hogg  UW  CSE  P501
Prog
Prog
;
Stm
Stm
AsStm
IfStm
Var
=
Exp
then
Exp
if
AsStm
a
VorC
Var
=
Exp
VorC
+
VorC
Const
VorC
b
Var
Const
Const
1
1
a
2
Jim Hogg  UW  CSE  P501
Prog
=
IfStm
Var:a
Const:1
+
=
Const:2
Var:b
Var:a
Const:1
Jim Hogg  UW  CSE  P501
Try to invent a regex description for arithmetic expressions: singlecharacter variable names; operators +  and [Note: red denotes terminals, below]
v = [az] // variable
o = +     // operator
v ( o v )* // derives a + b cok, but now add ( )
(? v (o v )? )* // derives (a + b) c
// but also gibberish like:a + b) c (
Almost every programming language includes such balanced pairs: ( ), { }, begin end. Conclusion: regex won’t work.
More generally, regex correspond to DFAs. They can only ‘count’ pairs up to a finite limit.
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
The following CSG generates the language an bncn for n >= 1
Note: CSGs will not be discussed further, nor examined, as part of P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
S =>* x A z => x z =>* xyz
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
ExpExp Op Exp  Dig
Op +    *  /
Dig 0  1  2  3  4  5  6  7  8  9
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 1Give a leftmost derivation of 2+3*4; show the parse tree
Exp
Exp
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 2Exp
Exp
=> Exp+ Exp
Exp
Exp
+
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 3Exp
Exp
Exp => Exp+ Exp
=> Dig + Exp
Exp
Exp
Dig
+
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 4Exp
Exp
Exp => Exp+Exp
=> Dig + Exp
=> 2 + Exp
Exp
Exp
Dig
+
2
Jim Hogg  UW  CSE  P501
Exp ::= Exp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig::= [09]
2+3*4 – part 5Exp
Exp
Exp => Exp+ Exp
=> Dig + Exp
=> 2 +Exp
=> 2 + Exp * Exp
Exp
Exp
Exp
Exp
Dig
2
+
*
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 6Exp
Exp
Exp => Exp+ Exp
=> Dig + Exp
=> 2 +Exp
=> 2 + Exp * Exp
=> 2 + Dig * Exp
Exp
Exp
Exp
Exp
Dig
Dig
2
*
+
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 7Exp
Exp
Exp => Exp+ Exp
=> Dig + Exp
=> 2 +Exp
=> 2 + Exp* Exp
=> 2 + Dig * Exp
=> 2 + 3 * Exp
Exp
Exp
Exp
Exp
Dig
Dig
2
+
3
*
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 8Exp
Exp
Exp => Exp+ Exp
=> Dig + Exp
=> 2 +Exp
=> 2 + Exp* Exp
=> 2 + Dig * Exp
=> 2 + 3 * Exp
=> 2 + 3 * Dig
Exp
Exp
Exp
Exp
Dig
Dig
Dig
2
3
*
+
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 9Exp
Exp
Exp => ExpOp Exp
=> Dig Op Exp
=> 2 OpExp
=> 2 + Exp
=> 2 + ExpOp Exp
=> 2 + Dig Op Exp
=> 2 + 3 OpExp
=> 2 + 3 * Exp
=> 2 + 3 * Dig
=> 2 + 3 * 4
Exp
Exp
Exp
Op
Exp
Dig
Dig
Dig
2
3
*
4
+
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
2+3*4 – part 10Give a different leftmost derivation of 2+3*4
Exp
Exp
=> Exp * Exp
=> Exp + Exp * Exp
=> 2 + Exp * Exp
=> 2 + 3 *Exp
=> 2 + 3 * 4
Exp
Exp
Exp
Exp
Dig
Dig
Dig
4
2
+
3
*
Jim Hogg  UW  CSE  P501
*
+
4
*
+
2
3
4
2
3
Result = 2 + (3 * 4) = 14
Result = (2 + 3) * 4 = 20
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
Another exampleJim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
Another example
result = 6
Give two different rightmost derivations of 5 – 6 – 7
Exp => Exp  Exp
=> Exp  Exp  Exp
=> Exp  Exp 7
=> Exp 6  7
=> 5  6  7
5

6
7
Exp => Exp Exp
=> Exp 7
=> Exp  Exp 7
=> Exp 6  7
=> 5  6  7
result = 8

7

6
5
Jim Hogg  UW  CSE  P501
ExpExp + Exp  Exp – Exp  Exp * Exp
 Exp / Exp  Dig
Dig [09]
Another example
result = 6
Give two different leftmost derivations of 5 – 6 – 7
Exp => Exp Exp
=> 5  Exp
=> 5  Exp Exp
=> 5  6  Exp
=> 5  6  7
5

6
7
result = 8

Exp => Exp Exp
=> Exp Exp  Exp
=> 5  Exp  Exp
=> 5  6  Exp
=> 5  6  7
7

6
5
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
expexp + term  exp – term  term
term term * factor  term / factor  factor
factor int  ( exp )
int 0  1  2  3  4  5  6  7
E E + T  E – T  T
T T * F  T / F  F
F ( E )  D
D [09]
Jim Hogg  UW  CSE  P501
T T * F  T / F  F
F ( E )  D
D [09]
Derive 2 + 3 * 4E => E + T
=> E + T * F
=> E + T * D
=> E + T * 4
=> E + F * 4
=> E + D * 4
=> E + 3 * 4
=> T + 3 * 4
=> F + 3 * 4
=> D + 3 * 4
=> 2 + 3 * 4
+
*
2
4
3
Result = 2 + (3 * 4) = 14
This grammar yields the correct, expected (school algebra) result
Jim Hogg  UW  CSE  P501
T T * F  T / F  F
F ( E )  D
D [09]
Derive 5  6  7E => E  T
=> E  F
=> E  D
=> E  7
=> E  T  7
=> E  F  7
=> E  D  7
=> E  6  7
=> F  6  7
=> D  6  7
=> 5  6  7
result = 8

7

6
5
Jim Hogg  UW  CSE  P501
stm if ( cond ) stm
 if ( cond ) stm else stm
“The Dangling Else”  a 'weakness' in C, Pascal, etc
Jim Hogg  UW  CSE  P501
stmif ( cond ) stm
 if ( cond ) stmelse stm
stm
if
cond
)
stm
(
if (cond) if (cond) stm else stm
if
stm
else
)
cond
(
stm
stm
if
stm
else
)
cond
(
stm
if
cond
)
stm
(
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
StmIfElse  IfNoElse
IfElse if ( Exp ) IfElse else IfElse
IfNoElse if ( Exp ) Stm
 if ( Exp ) IfElse else IfNoElse
Jim Hogg  UW  CSE  P501
IfStm if Exp then Stm end
 if Exp then Stm else Stm end
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501
Jim Hogg  UW  CSE  P501