0% found this document useful (0 votes)
53 views9 pages

CD Important Questions

The document discusses the role of parsers and compiler tools like YACC. It describes operator precedence parsing and different error recovery strategies parsers can use like panic mode, statement mode, error productions, and global correction. It also defines translation schemes and abstract syntax trees. Syntax directed translation is explained as augmenting context-free grammars with semantic rules to perform semantic analysis during parsing by propagating attributes.

Uploaded by

ganesh moorthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views9 pages

CD Important Questions

The document discusses the role of parsers and compiler tools like YACC. It describes operator precedence parsing and different error recovery strategies parsers can use like panic mode, statement mode, error productions, and global correction. It also defines translation schemes and abstract syntax trees. Syntax directed translation is explained as augmenting context-free grammars with semantic rules to perform semantic analysis during parsing by propagating attributes.

Uploaded by

ganesh moorthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

2 Marks

1.What is the role of parser?


Parser is a compiler that is used to break the data into smaller elements coming from lexical
analysis phase. A parser takes input in the form of sequence of tokens and produces output in the
form of parse tree.

2. What is the purpose of YACC?


Yacc (yet another compiler compiler) is a grammar parser and parser generator. That is, it is a
program that reads a grammar specification and generates code that is able to organize input
tokens in a syntactic tree in accordance with the grammar.

3. What are the disadvantages of operator precedence parsing?


 Operator Like minus can be unary or binary. So, this operator can have different precedence's in
different statements.
 Operator Precedence Parsing applies to only a small class of Grammars.

4. What are various stages that a parser can recover from a syntactic error?
 Panic mode. ...
 Statement mode. ...
 Error productions. ...
 Global correction. ...
 Abstract Syntax Trees

5. What is a translation scheme?


A TRANSLATION SCHEME is a context-free grammar in which semantic rules are embedded
within the right sides of the productions.

13 & 14 Marks
1.Error recovery strategies in parsing.
A parser should be able to detect and report any error in the program. It is expected that when an
error is encountered, the parser should be able to handle it and carry on parsing the rest of the
input. Mostly it is expected from the parser to check for errors but errors may be encountered at
various stages of the compilation process. A program may have the following kinds of errors at
various stages:
 Lexical : name of some identifier typed incorrectly
 Syntactical : missing semicolon or unbalanced parenthesis
 Semantical : incompatible value assignment
 Logical : code not reachable, infinite loop
There are four common error-recovery strategies that can be implemented in the parser to deal
with errors in the code.
Panic mode
When a parser encounters an error anywhere in the statement, it ignores the rest of the statement
by not processing input from erroneous input to delimiter, such as semi-colon. This is the easiest
way of error-recovery and also, it prevents the parser from developing infinite loops.
Statement mode
When a parser encounters an error, it tries to take corrective measures so that the rest of inputs of
statement allow the parser to parse ahead. For example, inserting a missing semicolon, replacing
comma with a semicolon etc. Parser designers have to be careful here because one wrong
correction may lead to an infinite loop.
Error productions
Some common errors are known to the compiler designers that may occur in the code. In
addition, the designers can create augmented grammar to be used, as productions that generate
erroneous constructs when these errors are encountered.
Global correction
The parser considers the program in hand as a whole and tries to figure out what the program is
intended to do and tries to find out a closest match for it, which is error-free. When an erroneous
input (statement) X is fed, it creates a parse tree for some closest error-free statement Y. This
may allow the parser to make minimal changes in the source code, but due to the complexity
(time and space) of this strategy, it has not been implemented in practice yet.
Abstract Syntax Trees
Parse tree representations are not easy to be parsed by the compiler, as they contain more details
than actually needed. Take the following parse tree as an example:
If watched closely, we find most of the leaf nodes are single child to their parent nodes. This
information can be eliminated before feeding it to the next phase. By hiding extra information,
we can obtain a tree as shown below:

Abstract tree can be represented as:

ASTs are important data structures in a compiler with least unnecessary information. ASTs are
more compact than a parse tree and can be easily used by a compiler.

2. Explain about the syntax directed translation.


Parser uses a CFG(Context-free-Grammar) to validate the input string and produce output for
the next phase of the compiler. Output could be either a parse tree or an abstract syntax tree.
Now to interleave semantic analysis with the syntax analysis phase of the compiler, we use
Syntax Directed Translation.

Conceptually, with both syntax-directed definition and translation schemes, we parse the input
token stream, build the parse tree, and then traverse the tree as needed to evaluate the semantic
rules at the parse tree nodes. Evaluation of the semantic rules may generate code, save
information in a symbol table, issue error messages, or perform any other activities. The
translation of the token stream is the result obtained by evaluating the semantic rules.
Definition
Syntax Directed Translation has augmented rules to the grammar that facilitate semantic
analysis. SDT involves passing information bottom-up and/or top-down to the parse tree in
form of attributes attached to the nodes. Syntax-directed translation rules use 1) lexical values
of nodes, 2) constants & 3) attributes associated with the non-terminals in their definitions.

The general approach to Syntax-Directed Translation is to construct a parse tree or syntax tree
and compute the values of attributes at the nodes of the tree by visiting them in some order. In
many cases, translation can be done during parsing without building an explicit tree.
Example

E -> E+T | T
T -> T*F | F
F -> INTLIT
This is a grammar to syntactically validate an expression having additions and multiplications
in it. Now, to carry out semantic analysis we will augment SDT rules to this grammar, in order
to pass some information up the parse tree and check for semantic errors, if any. In this
example, we will focus on the evaluation of the given expression, as we don’t have any
semantic assertions to check in this very basic example.

E -> E+T { E.val = E.val + T.val } PR#1


E -> T { E.val = T.val } PR#2
T -> T*F { T.val = T.val * F.val } PR#3
T -> F { T.val = F.val } PR#4
F -> INTLIT { F.val = INTLIT.lexval } PR#5
For understanding translation rules further, we take the first SDT augmented to [ E -> E+T ]
production rule. The translation rule in consideration has val as an attribute for both the non-
terminals – E & T. Right-hand side of the translation rule corresponds to attribute values of the
right-side nodes of the production rule and vice-versa. Generalizing, SDT are augmented rules
to a CFG that associate 1) set of attributes to every node of the grammar and 2) a set of
translation rules to every production rule using attributes, constants, and lexical values.
Let’s take a string to see how semantic analysis happens – S = 2+3*4. Parse tree corresponding
to S would be
To evaluate translation rules, we can employ one depth-first search traversal on the parse tree.
This is possible only because SDT rules don’t impose any specific order on evaluation until
children’s attributes are computed before parents for a grammar having all synthesized
attributes. Otherwise, we would have to figure out the best-suited plan to traverse through the
parse tree and evaluate all the attributes in one or more traversals. For better understanding, we
will move bottom-up in the left to right fashion for computing the translation rules of our
example.
The above diagram shows how semantic analysis could happen. The flow of information
happens bottom-up and all the children’s attributes are computed before parents, as discussed
above. Right-hand side nodes are sometimes annotated with subscript 1 to distinguish between
children and parents.
Additional Information
Synthesized Attributes are such attributes that depend only on the attribute values of children
nodes.
Thus [ E -> E+T { E.val = E.val + T.val } ] has a synthesized attribute val corresponding to
node E. If all the semantic attributes in an augmented grammar are synthesized, one depth-first
search traversal in any order is sufficient for the semantic analysis phase.
Inherited Attributes are such attributes that depend on parent and/or sibling’s attributes.
Thus [ Ep -> E+T { Ep.val = E.val + T.val, T.val = Ep.val } ], where E & Ep are same
production symbols annotated to differentiate between parent and child, has an inherited
attribute val corresponding to node T.
Advantages of Syntax Directed Translation:

Ease of implementation: SDT is a simple and easy-to-implement method for translating a


programming language. It provides a clear and structured way to specify translation rules using
grammar rules.
Separation of concerns: SDT separates the translation process from the parsing process,
making it easier to modify and maintain the compiler. It also separates the translation concerns
from the parsing concerns, allowing for more modular and extensible compiler designs.
Efficient code generation: SDT enables the generation of efficient code by optimizing the
translation process. It allows for the use of techniques such as intermediate code generation
and code optimization.

Disadvantages of Syntax Directed Translation:

Limited expressiveness: SDT has limited expressiveness in comparison to other translation


methods, such as attribute grammars. This limits the types of translations that can be
performed using SDT.
Inflexibility: SDT can be inflexible in situations where the translation rules are complex and
cannot be easily expressed using grammar rules.
Limited error recovery: SDT is limited in its ability to recover from errors during the
translation process. This can result in poor error messages and may make it difficult to locate
and fix errors in the input program.

3. Explain about the LR parsing algorithm


LR parser is a bottom-up parser for context-free grammar that is very generally used by
computer programming language compiler and other associated tools. LR parser reads their
input from left to right and produces a right-most derivation. It is called a Bottom-up parser
because it attempts to reduce the top-level grammar productions by building up from the
leaves. LR parsers are the most powerful parser of all deterministic parsers in practice.
Description of LR parser :
The term parser LR(k) parser, here the L refers to the left-to-right scanning, R refers to the
rightmost derivation in reverse and k refers to the number of unconsumed “look ahead” input
symbols that are used in making parser decisions. Typically, k is 1 and is often omitted. A
context-free grammar is called LR (k) if the LR (k) parser exists for it. This first reduces the
sequence of tokens to the left. But when we read from above, the derivation order first extends
to non-terminal.
1. The stack is empty, and we are looking to reduce the rule by S’→S$.
2. Using a “.” in the rule represents how many of the rules are already on the stack.
3. A dotted item, or simply, the item is a production rule with a dot indicating how much RHS
has so far been recognized. Closing an item is used to see what production rules can be
used to expand the current structure. It is calculated as follows:
Rules for LR parser :
The rules of LR parser as follows.
1. The first item from the given grammar rules adds itself as the first closed set.
2. If an object is present in the closure of the form A→ α. β. γ, where the next symbol after
the symbol is non-terminal, add the symbol’s production rules where the dot precedes the
first item.
3. Repeat steps (B) and (C) for new items added under (B).
LR parser algorithm :
LR Parsing algorithm is the same for all the parser, but the parsing table is different for each
parser. It consists following components as follows.
1. Input Buffer –
It contains the given string, and it ends with a $ symbol.

2. Stack –
The combination of state symbol and current input symbol is used to refer to the parsing
table in order to take the parsing decisions.
Parsing Table :
Parsing table is divided into two parts- Action table and Go-To table. The action table gives a
grammar rule to implement the given current state and current terminal in the input stream.
There are four cases used in action table as follows.
1. Shift Action- In shift action the present terminal is removed from the input stream and the
state n is pushed onto the stack, and it becomes the new present state.
2. Reduce Action- The number m is written to the output stream.
3. The symbol m mentioned in the left-hand side of rule m says that state is removed from the
stack.
4. The symbol m mentioned in the left-hand side of rule m says that a new state is looked up
in the goto table and made the new current state by pushing it onto the stack.
An accept - the string is accepted
No action - a syntax error is reported
Note –
The go-to table indicates which state should proceed.
LR parser diagram :

You might also like