LL1 Parser S Motogna FLCD Linear algorithm S
- Slides: 16
LL(1) Parser S. Motogna - FL&CD
Linear algorithm S. Motogna - FL&CD
FIRSTk • ≈ first k terminal symbols that can be generated from �� FOLLOWk • ≈ next k symbols generated after/ following A S. Motogna - FL&CD
LL(k) Principle • L = left (sequence is read from left to right) • L = left (use leftmost derivation) • Prediction of length k ai+1…ai+k • In any moment of parsing, acțion is uniquely determinde by: • Closed part (a 1…ai) • Current symbol A • Prediction ai+1…ai+k (length k) S. Motogna - FL&CD
Definition • A cfg is LL(k) if for any 2 leftmost derivation we have: st st such that then S. Motogna - FL&CD
Theorem The necessary and sufficient condition for a grammar to be LL (k) is that for any pair of distinct productions of a nonterminal (A→ �� , A→�� , �� ≠�� ) the condition holds: * such that FIRSTk(���� ) ⋂ FIRSTk(���� )= �� , ∀�� astfel încât S => u. A�� Theorem: A grammar is LL(1) if and only if for any nonterminal A with productions A →�� , FIRST(�� 1| �� 2|. . . | �� n , FIRST(�� i) ∩ FIRST(�� j) = ∅ and if �� i⇒�� i) ∩ FOLLOW(A)= ∅, ∀i, j = 1, n, i≠j S. Motogna - FL&CD
LL(1) Parser • Prediction of length 1 • Steps: Executed 1 time 1) construct FIRST, FOLLOW 2) Construct LL(1) parse table 3) Analyse sequence based on moves between configurations S. Motogna - FL&CD
Step 2: Construct LL(1) parse table • Possible action depend on: • Current symbol ∈ N∪�� • Possible prediction ∈ �� • Add a special character “$” ( ∉ N∪�� ) – marking for “empty stack” = > table: • One line for each symbol ∈ N∪�� ∪{$} • One column for each symbol ∈ �� ∪{$} S. Motogna - FL&CD
Rules LL(1) table production in P with number i if production in P with number i (error) otherwise S. Motogna - FL&CD
Remark A grammar is LL(1) if the LL(1) parse table does NOT contain conflicts – there exists at most one value in each cell of the table M(A, a) S. Motogna - FL&CD
Step 3: Definire configurations and moves • INPUT: • Language grammar G = (N, �� , P, S) • LL(1) parse table • Sequence to be parsed w =a 1…an • OUTPUT: If (w ∈L(G)) then string of productions else error & location of error S. Motogna - FL&CD
LL(1) configurations Initial configuration: (w$, S$, �� ) (�� , �� ) where: • �� = input stack • �� = working stack • �� = output (result) Final configuration: ($, $, �� ) S. Motogna - FL&CD
Moves 1. Push – put in stack if (pop A and push symbols of �� ) 2. Pop – take off from stack (from both stacks) if 3. Accept 4. Error - otherwise S. Motogna - FL&CD
Algorithm LL(1) parsing • INPUT: § LL(1) table with NO conflicts; § G –grammar (productions) § Input sequence w = a 1 a 2. . . An • OUTPUT: § sequence accepted or not? § If yes then string of productions S. Motogna - FL&CD
Algorithm LL(1) parsing (cont) alfa : = w$; beta : = S$; pi : = ɛ; go : = true; while go do if M(head(beta), head(alfa))=(b, i) then pop(beta); push(beta, b); add(pi, i) else if M(head(beta), head(alfa))=pop then pop(beta); pop(alfa); else if M(head(beta), head(alfa))=acc then go: =false; s: =”acc”; else go: =false; s: =”err”; if s=”’acc”’ then end if write(”Sequence accepted”); end if write(pi) else end if end while write(” Sequence not accepted”) S. Motogna - FL&CD
Remarks 1) LL(1) parser provides location of the error 2) Grammars can be transformed to be LL(1) example: I -> if C then S | if C then S else S // is not LL(1) I -> if C then S T T -> ɛ | else S // is LL(1) S. Motogna - FL&CD
- Ll1
- Sllgen
- Predictive grammar
- Ll1
- Soa-ll1
- Top down parsing vs bottom up
- Soa-ll1
- Ll1
- Diff between a* and ao*
- Sweep line algorithm cp algorithm
- Msxsl.exe
- Drew davidson ku
- Eliminate left recursion calculator
- Recursive descent parser
- Recursive descent parsing
- Limitations of recursive descent parser
- Lr parsers are attractive because