The document explains First and Follow sets in the context of LL(1) parsing, detailing how they help determine which production rules to apply in a grammar. It provides examples illustrating the importance of these sets in resolving ambiguities and ensuring correct parsing decisions. Additionally, it outlines the conditions for a grammar to be classified as LL(1), including the absence of left recursion and the necessity for left factoring.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
16 views
3 LLK First and Follow
The document explains First and Follow sets in the context of LL(1) parsing, detailing how they help determine which production rules to apply in a grammar. It provides examples illustrating the importance of these sets in resolving ambiguities and ensuring correct parsing decisions. Additionally, it outlines the conditions for a grammar to be classified as LL(1), including the absence of left recursion and the necessity for left factoring.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20
Chapter III
LIV E
First and Follow sets and LL(K)
parsing Prepared by Khaled Dassouki First and Follow sets • First Set: The First set of a non-terminal contains all the terminal symbols that can appear at the beginning of any string derived from that non-terminal. In other words, it tells us which terminal symbols are possible when expanding a non-terminal. • For example, if we have a rule like A -> aB | ε, then the First set of A includes the terminal a, as well as ε, which represents the possibility of deriving an empty string. First and Follow sets • The Follow set of a non-terminal contains all terminals that can immediately follow it in any derivation of the grammar. • This means that if a non-terminal appears in the middle of a sentence, the Follow set tells us what symbol might come right after it. • For example, in a rule like S -> AB, the Follow set of A might include anything that can follow B, depending on other rules in the grammar. • Follow sets are especially useful for parsers when making decisions about which rule to apply next and ensuring that we’re interpreting the structure of a sentence correctly, especially for LL(1) parsers where we only look one symbol ahead. Example: Why First and Follow? Consider the Grammar: •S→Aa|Bb •A→ε •B→ε If the input string is “a”, the parser starts with S, but when choosing between A → ε and B → ε, it has no clue which to pick. This leads to ambiguity. Example: Why First and Follow? • FIRST(A) = { ε } • FIRST(B) = { ε } • FIRST(A a) = { a } • FIRST(B b) = { b } Since FIRST(A a) = { a } and FIRST(B b) = { b }, the parser now knows that S → A a should be chosen if a is in input. Example: Why First and Follow? • FOLLOW(S) = { $, a, b } • FOLLOW(A) = { a } • FOLLOW(B) = { b } FOLLOW ensures that A → ε is used only when “a” follows, and B → ε is used only when b follows. Example: Why First and Follow? •S→AB •A→ε|x •B→y FIRST(A) = { ε, x } FIRST(B) = { y } FOLLOW(A) = { y } If the input is "y", the parser must decide between: A → ε or A → x Since y ∈ FOLLOW(A), we choose A → ε, ensuring correct parsing. Why using First and Follow? • FIRST helps decide which production to use. • FOLLOW ensures ε-productions are applied only when necessary. • Without them, an LL(1) parser may make incorrect or ambiguous choices. LL(1) Grammar An LL(1) grammar is a type of context-free grammar (CFG) that can be parsed by a recursive descent parser without backtracking. • L → Left-to-right scanning of the input. • L → Leftmost derivation of the parse tree. • 1 → One lookahead token is sufficient to decide which production to apply. Conditions for LL(1) Grammar • A grammar is LL(1) if the parser can determine the correct production rule based on a single lookahead token. Conditions for LL(1) Parsers – No left recursion The grammar should not have direct or indirect left recursion because left-recursive rules cause infinite recursion in a recursive descent parser.
Example of left recursion (not LL(1)):
A→Aα|β Fixed (by eliminating left recursion): A → β A‘ A' → α A' | ε Conditions for LL(1) Parsers – Left Factored Grammar If a nonterminal has multiple productions that start with the same symbol, left factoring is required. Example (not LL(1)): A→aB|aC Fixed (Left Factoring): A→aX X→B|C Conditions for LL(1) Parsers – (FIRST and FOLLOW Set Condition) For each nonterminal A, if it has multiple productions: A→α|β The grammar is LL(1) if and only if: {FIRST(α)} UNION {FIRST(β)} = Empty If one of the productions derives ε, then: FIRST(α) UNION FOLLOW(A) = Empty
(Lecture Notes in Computer Science 6547 Lecture Notes in Artificial Intelligence) Terrance Swift (Auth.), Salvador Abreu, Dietmar Seipel (Eds.) - Applications of Declarative Programming and Knowledge