Syntax Analysis
Context Free Grammars:
Is a tuple
- : Set of non terminals
- : Set of terminals
- : Start Symbol
- : Are set of rules that map non-terminals to a set of terminals and non-terminals
Reduced and Extended CFGs:
A non terminal is
- reachable iff: there exists such that
- productive iff:
A grammar is a reduced grammar if it has neither unreachable nor non-productive non-terminals.
A grammar is said to be an extended grammar if we add a non terminal and add the production .
Derivations:
Expanding a nonterminal according to the productions defined by the grammar is derivation
( and derives ) iff there exists and such that
Using this we can write for a Language defined by G
Leftmost and Rightmost derivations:
Given a context free grammar
In left most derivation we derive the left most non-terminal ( and left derives ) iff there exists ; ; and such that
In right most derivation we derive the right most non-terminal ( and right derives ) iff there exists ; ; ; and such that
Ambiguous Grammar:
A grammar that has
- two left most derivation for the same string
- two right most derivation for the same string
- two syntax trees for the same string is called ambiguous.
It is undecidable if the grammar is ambiguous.
Pushdown Automata:
A PDA is defined using a tuple
- : Input alphabet
- : Finite set of states (Stack Symbols)
- : Initial State
- : Set of Final states
- : (Reads one input alphabet and pops multiple items from the stack, pushes none or multiple items on the stack)
for , , is a configuration The binary relation step on configuration is defined by: if there exists such that
The language accepted by PDA can be then defined as
Context Free Items and Item pushdown automata:
An item is denoted as is a complete item
Item Pushdown Automata: Given a CFG we create its extended version by adding the nonterminal and adding a production
Then the item pushdown automata can be defined as
TOP DOWN Parser:
Read input from left to right. Construct the syntax tree in a top-down manner sstarting with a node labeled with the start symbol.
Recursive Descent Parsing: Uses sets to make expansion deterministic
: returns the k-length prefix of the concatenation of its operand strings
The set for a particular production contains the set of 1-length prefix of the string that they can generate. This allows us to use make deterministic choice as to which production to use while parsing a given string.
In cases where the given production may also generate an empty-string we also have to consider which character might follow the string that can be generated by the production, and thus is also used.
LL(k) Grammar:
Given a grammar is an where is a natural number, iff the following holds
if then
In simple terms we dont want two distinct productions for a non-terminal to yield two strings that have a same k-length prefix.
Recursive and LL(K): A non-terminal is called left recursive if A CFG is left-recursive if it contains at least one left-recursive non-terminal is not if it is left-recursive
BOTTOM UP PARSER:
Bottom up parsing by pruning handles:
Handle:
Leftmost set of adjacent leaves forming a complete branch.
Suppose , then while parsing a string that was generated using the above production, there will come an instance where the AST has the non-terminal having children . This is then an handle.
While parsing we will recursively prune handles. This corresponds to a reverse rightmost derivation.
Now we need a way that by successively reading the input we want to identify the handles, which will allow us to do reduction.
For this we create a characteristic FSM of .
Characteristic FSM of :
a characteristic FSM of is a NFSM
- : the states of the FSM are the items of the
- : the alphabet is the set of non-terminals and terminals
- is the initial state and
- is a set of complete items. Used to identify handles
To draw the :
We will first write the extended version of the grammar. Then we write each production in in item format.
Ex: for a production we will draw the states for this production as
Then we will add epsilon transitions from the item of the form to all the states of
We will then make the NFSM into DFSM by using subset construction. This deterministic version is denoted as
When we do subset construction some of the items will be merged into a single state.
Building the LR(0) Pushdown Automaton:
interpreted as a PDA
- : Stack Alphabet
- : Input Alphabet
- : the start state of
- : the final state of
- :
shift:
if is defined Consume the input and push the successor state of under .
reduce:
, if at top of the stack. We pop elements from the stack and push the new stack under onto the stack.
Non determinism in :
States with shift-reduce conflict:
Have atleast one read item and one complete item
States with reduce-reduce conflict:
Have atleast two complete items
A state with conflicts is inadequate.
Adding Lookahead:
a item is , a item is , the here is the lookahead set.
LR Parser:
Take their decision to shift or reduce by consulting:
- Top of the stack
- Lookahead
These are used as an input for the Action table and the appropriate action is given by
A goto table records the transition function of a characteristic automaton .
