Unit Ii QB
Unit Ii QB
Part- A
1. Consider the following grammar and demonstrate that the grammar is ambiguous
by showing two different parse trees for some string. (Nov/Dec 2020)
S→ AB |aaB
A→ a|Aa
B→b
A->SdA`
A`->cA`|∈
4. What is meant by coercion? (Nov/Dec 2020)
The Argument Coercion is one technique by which the compiler can implicitly
convert the arguments from one type to another type. It follows argument promotion
rule. If one argument is lower datatype, that can be converted into higher datatypes, but
the reverse is not true.
%%
/* rules */
....
%%
/* auxiliary routines */
....
The handle of the right sequential form Y is the production of Y where the string
S may be found and replaced by A to produce the previous right sequential form
in RMD(Right Most Derivation) of Y.
Sentential form: S => a here, ‘a’ is called sentential form, ‘a’ can be a mix of
terminals and nonterminals.
16. How do you identify predictive parser and non-recursive predictive parser? (Nov/Dec
2021)
Predictive parsing is a special form of recursive descent parsing, where no
backtracking is required, so this can predict which products to use to replace the
input string.
Non-recursive predictive parsing or table-driven is also known as LL(1) parser.
This parser follows the leftmost derivation (LMD).
17. For what type of grammar, recursive descent parser cannot be constructed? Show the
steps involved in recursive descent parsing with backtracking for the string cad with
the given grammar S->cAd, A->ab|a (April/May 2023)
S->cAd
S->cad
The above grammar has no left recursion.
18. Construct a parse tree and syntax tree for 4-6/3*5+7 (April/May 2023)
21. What are the goals of error handler in a parser? (Nov/Dec 2023)
The goals of an error handler in a parser are:
1. Detection of Errors: Identify syntactical errors in the source code as early as possible.
2. Recovery from Errors: Attempt to recover from errors so that parsing can continue and
more errors can be detected in a single pass.
3. Reporting Errors:** Provide meaningful and clear error messages that help the
programmer understand the nature and location of the errors.
4. Preserving the Parsing Process:** Ensure that the parser continues to operate correctly
and reaches a valid conclusion, even after encountering errors.
5. Minimizing the Impact of Errors: Prevent cascading errors by correcting or ignoring
errors in a way that minimizes their impact on the rest of the parsing process.
Parsing Table
a b c d e $ S A B
0 S2 S4 S5 1 3
1 Accept
2 S4 6,3
3 S7
4 r3 r3
5 r4
6 S5 8
7 S9
8 S10
9 r2 r2
10 r1
String abbcde
Stack Move of Input Tape Output
$S abbcde$ S->aABe
$e e$ pop
$ $ Accept
2. Construct LL(1) parsing table for the following grammar using FIRST and
FOLLOW set. (Nov/Dec 2020)
S→ UVW
U→ (S) | aSb |d
V → aV| e
W → cW | e
Give the parsing actions for the input string “(dc)ac”.
Parsing Table
( ) a d b c e $
S S->UVW S->UVW S->UVW
U U->(S) U->aSb U->d
V V->aV V->e
W W->cW W->e
String (dc)ac
Stack Move of Input Tape Output
$S (dc)ac$ S->UVW
3. Construct a predictive parser for the following grammar. (Nov/Dec 2020) (April/May
2022) (Nov/Dec 2021)
S → (L) | a
L→ L, S | S. String (a,(a,(a,a)))
L->SL’
L’->,SL’|∈
Parsing Table
( ) a , $
String (a,(a,(a,a)))
Stack Move of Input Tape Output
$S (a,(a,(a,a)))$ S->(L)
$)L’ )$ L’->∈
$) )$ pop
$ $ Accept
4. Explain the operator precedence parsing with suitable example. (Nov/Dec 2020)
Operator precedence grammar is kinds of shift reduce parsing method. It is
applied to a small class of operator grammars. A grammar is said to be operator
precedence grammar if it has two properties:
No R.H.S. of any production has a∈.
No two non-terminals are adjacent.
Operator precedence can only established between the terminals of the grammar. It
ignores the non-terminal.
There are the three operator precedence relations:
a ⋗ b means that terminal "a" has the higher precedence than terminal "b".
a ⋖ b means that terminal "a" has the lower precedence than terminal "b".
a ≐ b means that the terminal "a" and "b" both have same precedence.
Precedence table:
Parsing Action
Both end of the given input string, add the $ symbol.
Now scan the input string from left right until the ⋗ is encountered.
Scan towards left over all the equal precedence until the first left most ⋖ is
encountered.
Everything between left most ⋖ and right most ⋗ is a handle.
$ on $ means parsing is successful
5. Consider the following grammar and construct predictive parser. (Nov/Dec 2019)
E → E + T/T, T → T * F|F, F → (E) |id.
Check id+id*id
E’ –> +TE’/ε { +, ε } { $, ) }
T’ –> *FT’/ε { *, ε } { +, $, ) }
First Follow
Parsing Table
id + * ( ) $
String “id+id*id”
Stack Move of Input Tape Output
$E id + id * id $ E → T E'
$ E' T id + id * id $ T → FT'
$ E' T ' F id + id * id $ F → id
$ E' T ' id id + id * id $ POP
$ E' T ' + id * id $ T'→
$ E' + id * id $ E' → + T E'
$ E' T + + id * id $ POP
$ E' T id * id $ T → FT'
$ E' T ' F id * id $ F → id
$ E' T ' id id * id $ POP
$ E' T ' * id $ T '→ * F T '
$ E' T ' F * * id $ POP
$ E' T ' F id $ F → id
$ E' T ' id id $ POP
$ E' T ' $ T'→
$ E' $ E'→
$ STRING
$ ACCEPTED
6. Explain the usage of YACC parser generator in construction of a parser with one
example. (April/May 2023)
Each translation rule input to YACC has a string specification that resembles a
production of a grammar-it has a nonterminal on the LHS and a few alternatives on the
RHS. For simplicity, we will refer to a string specification as a production. YACC
generates an LALR(1) parser for language L from the productions, which is a bottom-
up parser. The parser would operate as follows: For a shift action, it would invoke the
scanner to obtain the next token and continue the parse by using that token. While
performing a reduced action in accordance with production, it would perform the
semantic action associated with that production.
Structure
/* definitions */
....
%%
/* rules */
....
%%
/* auxiliary routines */
....
Input File: Definition Part:
The definition part includes information about the tokens used in the syntax definition:
%token NUMBER
%token ID
Yacc automatically assigns numbers for tokens, but it can be overridden by
%token NUMBER 621
Yacc also recognizes single characters as tokens. Therefore, assigned token numbers
should no overlap ASCII codes.
The definition part can include C code external to the definition of the parser and
variable declarations, within %{ and %} in the first column.
It can also include the specification of the starting symbol in the grammar:
%start nonterminal
If yylex() is not defined in the auxiliary routines sections, then it should be included:
#include "lex.yy.c"
YACC input file generally finishes with:
.y
Output Files:
%{
#include <ctype.h>
#include <stdio.h>
#define YYSTYPE double /* double type for yacc stack */
%}
%%
Lines : Lines S '\n' { printf("OK \n"); }
| S '\n’
| error '\n' {yyerror("Error: reenter last line:");
yyerrok; };
S : '(' S ')’
| '[' S ']’
| /* empty */ ;
%%
#include "lex.yy.c"
void yyerror(char * s)
/* yacc error handler */
{
fprintf (stderr, "%s\n", s);
}
int main(void)
{
return yyparse();
}
Lex File (.l)
%{
%}
%%
[ \t] { /* skip blanks and tabs */ }
\n|. { return yytext[0]; }
%%
7. Explain LL(1) grammar for the sentence (April/May 2023)
S->iEts|iEtSeS|a
E->b
Parsing Table
i a e b $
S S->iEtSS S->a
S` S`->eS
S`-> ε
E E->b
Since there are more than one production for an entry in the table, the grammar is not
LL(1) grammar.
8. Briefly discuss about Design of a syntax analyzer for a sample language. (Nov/Dec
2023)
Defining the Grammar:
Start by defining the formal grammar of the language, typically using a
context-free grammar (CFG). The grammar consists of a set of production
rules that describe the syntax of valid statements in the language. For example,
a simple arithmetic expression grammar might include rules like:
expr → expr + term | term
term → term * factor | factor
factor → ( expr ) | number
Parser Implementation:
Implement the parser using the chosen technique. For a recursive descent
parser, write a set of recursive functions corresponding to each non-terminal in
the grammar. These functions call each other according to the production rules
to match the input tokens.
For example, a function parseExpr() might recursively call parseTerm()
and handle the "+" operation by matching tokens and building the parse tree.
Error Handling:
Implement error detection and recovery mechanisms. The parser should be
able to detect syntax errors and recover gracefully to continue parsing subsequent
parts of the input.
Building the Parse Tree:
As the parser processes the input, it can build a parse tree that represents the
syntactic structure of the code. This tree can later be used for further processing, such
as semantic analysis or code generation.