



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































Context-free grammars
Reject the input if it cannot be derived from the grammar
Parse tree for 5+1520:*
F ::= n
Grammar: (^) e
e
A set of tokens or terminals --- T
Atomic symbols in the language
A set of non-terminals --- NT
Variables representing constructs in the language
A set of productions --- P
Rules identifying components of a construct
BNF: each production has format A ::= B (or AB) where
A is a single non-terminal
B is a sequence of terminals and non-terminals
A start non-terminal --- S
The main construct of the language
e ::= n | e+e | e−e | e * e | e / e
Non-terminals: e
Terminal (token): n, +, -, *, /
Start symbol: e
n ::= d n | d
d ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Each replacement follows a production rule
One or more derivations for each program
Example: derivations for 5 + 15 * 20
e=> ee* => e+ee* => 5+ee* => 5+15e=>5+15**
e=> e+e => 5+e => 5+ee* =>5 +15e* => 5+15*
Example: different notations of expressions
Prefix + 5 * 15 20
Infix 5 + 15 * 20
Postfix 5 15 20 * +
Identifies only the meaningful components
The operation
The components of the operation
e
e
e
e
e
Parse Tree for
Abstract Syntax Tree for 5 + 15 * 20
Operators and keywords do not appear as leaves
They define the meaning of the interior (parent) node
Chains of single productions may be collapsed
If-then-else
cs5363 10
A grammar is syntactically ambiguous if
some program has multiple parse trees
Multiple choices of production rules during derivation
Consequence of multiple parse trees
Parse trees are used to interpret programs
Multiple ways to interpret a program
e
e
e
e e
e
e
e
e 5
Parse trees:
Grammar: e ::= n | e+e | e−e | e * e | e / e
Sentence: 5 + 15 * 20
Which of the following correctly defines non-ambiguous CFG?
(1) A grammar where each sentence has a unique syntax tree by leftmost
derivation
(2) A grammar where each sentence has a unique syntax tree by any
derivation
Solution1: introduce precedence and associativity rules to dictate
the choices of applying production rules
Original grammar
e ::= n | e+e | e−e | e * e | e / e
Precedence and associativity
* / >> + -
All operators are left associative
Derivation for n+n*n
e=>e+e=>n+e=>n+ee=>n+ne=>n+n*n
Solution2: rewrite production rules by introducing additional non-
terminals
Alternative grammar
E ::= E + T | E – T | T
T ::= T * F | T / F | F
F ::= n
Derivation for n + n * n
E=>E+T=>T+T=>F+T=>n+T=>n+TF=>n+FF=>n+nF=>n+nn
How to modify the grammar if
+ and - has high precedence than * and /
All operators are right associative
Ambiguous grammar with
disambiguating rules
E ::= E + E | E * E | (E) | id
F ::= (E) | id
vs.
Can change disambiguating rules without changing
productions
No need to reduce FTE, just to enforce disambiguating
rules
What languages can be defined by
CFG?
e ::= num | string | id | e+e
Cannot determine the type of variable names
Declaration of variables is in the context (symbol table)
Cannot ensure variables are always defined before used
int w;
0 = w;
for (w = 1; w < 100; w = 2w)
a = “c” + 3;
a = “c” + w
F ::= n
Grammar:
Left-most derivation for 5+1520-7:*
Which production rule to use for each left-most non-terminal?
e
e
e
recognize the language described by S
F ::= n
Grammar:
void ParseE() {
if (use the first rule) {
ParseE();
if (getNextToken() != PLUS)
ErrorRecovery()
ParseT();
}
else if (use the second rule) {
…
}
else …
}
void ParseT() { …… }
void ParseF() { …… }
How to decide which
production rule to use?
•Which to try first?
•Select a single one based on the
next input token
ν Infinite recursion without consuming any token cannot be
parsed by recursive descent parsers even with backtracking
A::=Aα | β
A::= β A’
A’::= αA’ | ε
F ::= n
Grammar:
Grammar:
E’ ::= + TE’ | - TE’ | ε
T’::= *FT’ | / FT’ | ε
F ::= n
Problem: Left-recursion could involve multiple derivations
Algorithm: Eliminating left-recursion
Input: grammar G with no cyclic
derivations (e.g. A⇒… ⇒ A) or ε-
production (e.g., A::= ε)
Output: equivalent grammar with no
left recursion
Algorithm:
some order A 1 ,A 2 ,…,An
for j = 1 to i-1 to
replace each production
Aj ::= β1 | β 2 | … | βk
end
eliminate immediate left-
recursion for all Ai productions
end
Example: S ::= Aa | b
A ::= Ac | Sd
Example: S ::= Aa | b
A ::= Ac | Aad | bd
Example: S ::= Aa | b
A ::= bdA’ | A’