SPCC Dec 2019
SPCC Dec 2019
Q.1
1. Enlist the different types of errors that are handled by Pass I and PassII of
assembler.
Pass I:
Syntax Errors: These errors occur due to viola ons of the language syntax rules, such as missing
semicolons, incorrect token placement, etc.
Label Errors: Pass I checks for errors related to labels, such as redefined labels, invalid label names, etc.
Opcode Errors: Errors related to incorrect or invalid opcodes are checked in this phase.
Operand Errors: Pass I verifies the correctness of operands, such as invalid addressing modes,
mismatched operand types, etc.
Symbol Table Errors: Errors related to symbol table management, such as duplicate symbol defini ons,
undefined symbols, etc.
Pass II:
Addressing Errors: Pass II checks for errors related to addressing, such as invalid or out-of-range
addresses.
Expression Errors: Errors related to expressions, such as arithme c errors, undefined symbols in
expressions, etc., are handled in this phase.
Machine Code Errors: Pass II is responsible for genera ng machine code, so any errors related to
genera ng machine code or encoding instruc ons are handled here.
Reloca on Errors: Errors related to reloca on of symbols, especially in cases of relocatable code, are
checked in Pass II.
Output File Errors: Any errors related to wri ng the output file, such as disk full, permission issues,
etc., are handled in this phase.
4. What is the need of Intermediate code genera on? Explain any two Intermediate
code genera on forms with example
Intermediate code genera on is an essen al phase in the compila on process that transforms the
source code into an intermediate representa on before further processing. This intermediate
representa on serves as a bridge between the high-level source code and the target machine code or
assembly language. There are several reasons for the need of intermediate code genera on:
Portability: Intermediate code allows the compiler to generate target code for different pla orms
without having to perform the en re compila on process again. Once the intermediate representa on
is generated, it can be op mized and translated into target code for various pla orms.
Op miza on: Intermediate code provides a convenient stage for applying op miza on techniques.
By performing op miza ons on the intermediate representa on, the compiler can improve the
efficiency of the generated code without modifying the original source code.
Simplify Compila on: Intermediate code simplifies the compila on process by separa ng the
concerns of syntax analysis and code genera on. It allows the compiler to focus on transla ng the
high-level language constructs into a simpler, pla orm-independent representa on before genera ng
the target code.
Ease of Debugging: Intermediate code can facilitate debugging by providing a more structured and
understandable representa on of the program's behavior. Debugging tools can analyze and
manipulate the intermediate code to iden fy and fix errors more effec vely.
Three-Address Code (TAC):
t1 = a + b
t2 = c * d
e = t1 - t2
In this example, t1, t2, and e are temporary variables used to store intermediate results, while a, b, c,
and d are variables or constants from the original source code. Each instruc on represents a single
opera on, such as addi on, mul plica on, or subtrac on, with one result and two operands.
Quadruples:
1. if a < b goto L1
2. t1 = c * d
3. goto L2
4. L1:
5. t1 = e - f
6. L2:
In this example, each quadruple represents an opera on or control flow instruc on, such as
comparison, mul plica on, subtrac on, and condi onal or uncondi onal jumps. The goto statements
transfer control to the specified labels (L1 and L2), allowing the program to execute different code
paths based on the result of the comparison opera on.
Q.2
1. What is Le factoring? Find FIRST & FOLLOW for the following grammar
S->Aa
A->B D
B->b | ϵ
D->d | ϵ
Le factoring is a grammar transforma on technique used to eliminate common prefixes from the
produc ons of a grammar. This technique helps to improve the efficiency of predic ve parsing
algorithms, such as LL(1) parsing, by resolving poten al conflicts.
To le factor a grammar, you iden fy common prefixes in the produc ons of a non-terminal and create
new non-terminals to represent those prefixes. Here's the le factored version of the given grammar:
Q.3
1. Explain YACC in detail.
Yacc (Yet Another Compiler Compiler) is a tool used in the field of compiler construc on for genera ng
parsers and lexical analyzers (scanners) based on a formal grammar specifica on. Yacc is part of the
Unix toolchain and is commonly used in conjunc on with Lex (or Flex) for complete compiler
construc on. Yacc takes a context-free grammar (CFG) as input and generates a parser in C or other
programming languages.
Working of Yacc:
Input Grammar: The user provides a context-free grammar (CFG) as input to Yacc. This grammar
describes the syntax rules of the programming language being processed.
Parser Genera on: Yacc generates a parser based on the provided grammar. The parser typically uses
LR(1) parsing technique, which is a bo om-up parsing method with one lookahead token.
Parsing Table: Yacc constructs a parsing table based on the grammar rules. This table is used by the
generated parser to parse the input and construct a parse tree or syntax tree.
Lexical Analysis: Typically, Yacc is used in combina on with Lex (or Flex), which generates the lexical
analyzer (scanner). Lex reads regular expressions and generates code that tokenizes the input based
on these expressions.
Interfacing with Lex: Yacc and Lex are o en used together in a compiler construc on project. Lex
generates tokens based on regular expressions and provides them to Yacc, which uses them in its
parsing process.
Parser Implementa on: Yacc generates parser code in C or other programming languages based on
the grammar specifica on. This code includes func ons for parsing and construc ng the parse tree
according to the grammar rules.
Seman c Ac ons: In addi on to parsing, Yacc allows the programmer to specify seman c ac ons
associated with grammar rules. These ac ons are executed when the corresponding rule is recognized
during parsing.
Error Handling: Yacc-generated parsers typically include error handling mechanisms to detect syntax
errors in the input and provide meaningful error messages to the user.
Features of Yacc:
1.Powerful Parsing: Yacc can handle complex context-free grammars and generate parsers capable of
parsing languages with intricate syntax rules.
2.Efficient Parsing: Yacc-generated parsers use efficient parsing techniques like LR(1) parsing, which
can parse input in linear me.
3.Modularity: Yacc promotes modularity by separa ng the syntax specifica on (grammar) from the
parser implementa on. This allows for easier maintenance and modifica on of the parser.
4.Error Repor ng: Yacc-generated parsers o en include robust error handling mechanisms, which
provide detailed error messages to aid in debugging.
5.Seman c Ac ons: Yacc allows programmers to specify seman c ac ons associated with grammar
rules, facilita ng the integra on of seman c analysis and code genera on phases in the compiler.