0% found this document useful (0 votes)
3 views

pcd 2 marks university

The document provides a comprehensive overview of concepts related to lexical analysis, parsing, and compiler design, including definitions of tokens, patterns, and lexemes, as well as error recovery methods and the structure of three-address code. It discusses various parsing techniques, including predictive parsing and LR parsing, and highlights the importance of semantic analysis and type checking in compilers. Additionally, it covers memory allocation strategies and the differences between stack and heap allocation.

Uploaded by

Rounakdeep Singh
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
3 views

pcd 2 marks university

The document provides a comprehensive overview of concepts related to lexical analysis, parsing, and compiler design, including definitions of tokens, patterns, and lexemes, as well as error recovery methods and the structure of three-address code. It discusses various parsing techniques, including predictive parsing and LR parsing, and highlights the importance of semantic analysis and type checking in compilers. Additionally, it covers memory allocation strategies and the differences between stack and heap allocation.

Uploaded by

Rounakdeep Singh
Copyright
© © All Rights Reserved
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/ 21

1. Define tokens, patterns, and lexemes.

o Token: A token is a category of lexemes, like identifier, number, or operator.

o Lexeme: An actual character sequence in source code matching a pattern.

o Pattern: A rule (often regular expression) that describes a token.

2. Classify approach used to recover errors in lexical analysis phase.

o Panic mode, deletion, insertion, replacement, and transposition are


used.

o These strategies help skip or correct unrecognized characters.

3. Write the three-address code for the statement a = b + c * 60.

ini

CopyEdit

t1 = c * 60

t2 = b + t1

a = t2

4. Point out why buffering is used in lexical analysis.

o Buffering reduces the number of I/O operations.

o It improves speed by reading chunks of characters instead of one at a time.

5. Define transition diagram for an identifier.

o A finite automaton where:

▪ Start → letter → [letter/digit]* → accepting state

o It recognizes valid identifiers like id1, abc123.


6. Compare Kleene closure and positive closure.

o Kleene Closure (*): Matches 0 or more repetitions.

o Positive Closure (+): Matches 1 or more repetitions.

7. State prefix, suffix, proper prefix, and proper suffix with an example.

o For string abc:

▪ Prefix: a, ab, abc

▪ Suffix: c, bc, abc

▪ Proper Prefix: a, ab

▪ Proper Suffix: c, bc

8. Define buffer pair.

o Two halves of a buffer used in lexical analysis for efficient reading.

o Each half has a sentinel to mark the end.

9. Differentiate the features of DFA and NFA.

o DFA: Deterministic, one transition per symbol.

o NFA: Non-deterministic, can have multiple transitions or ε-moves.

10. Identify the interactions between lexical analyzer and parser.

• The lexer provides the next token to the parser.

• The parser sends token requests and may give feedback for error recovery.

11. State parse tree and construct parse tree for –(id + id).

• Parse Tree: A tree showing syntactic structure.

• Tree for –(id + id):


bash

CopyEdit

()

/\

id id

12. Name the operations on languages.

• Union, Concatenation, Kleene Star, Intersection, Complement.

13. List out the phases of a compiler.

• Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code


Generation, Code Optimization, Code Generation, Symbol Table Management, Error
Handling.

14. Generalize the advantage of having sentinels in buffer pairs.

• Avoids repeated checks for end-of-buffer.

• Makes forward scanning efficient and error-free.

15. Analyze the role of semantic analyzer.

• Checks type rules, scope resolution, and ensures meaning correctness.

• Builds annotated syntax trees or symbol tables.

16. Illustrate the rules for three-address code generation.


• Break complex expressions into simple steps.

• Use temporary variables and simple operators.

17. Develop the structure of a lex program.

shell

CopyEdit

%{ Definitions %}

%%

Rules

%%

User code

18. Apply a grammar for branching statements.

bash

CopyEdit

stmt → if (expr) stmt | if (expr) stmt else stmt

19. Express the main idea of NFA and discuss with examples (a/b)*.

• NFA allows multiple transitions for a state and input.

• (a/b)* accepts ε, a, b, ab, ba, etc.

20. Define lex and give its execution steps.

• Lex: A lexical analyzer generator.

• Steps: Write lex program → run lex → compile with C compiler → execute scanner.

21. Differentiate interpreters and compilers.


• Interpreter: Executes line-by-line, slower, no object code.

• Compiler: Translates whole code, faster execution.

22. Apply the parse tree for the statement z := x + y * 130.

go

CopyEdit

:=

/ \

z +

/\

x *

/\

y 130

23. Outline the role of lexical analysis in compiler design.

• Converts source code into tokens.

• Removes whitespace/comments, detects lexical errors.

24. Criticize the use of input buffering with simple examples.

• Pros: Faster processing via block reads.

• Cons: Requires handling buffer boundaries and sentinels carefully.

1. Eliminate the left recursion for the grammar:

less

CopyEdit
S → Aa | b

A → Ac | Sd | ε

Solution:
Left recursion in A → Ac | Sd | ε
Remove by:

vbnet

CopyEdit

A → Sd A' | ε

A' → c A' | ε

2. Define handle pruning.

o Handle pruning is the process of reducing a right-sentential form to the start


symbol using handles (the rightmost derivable substring).

o It is used in bottom-up parsing.

3. Compute FIRST and FOLLOW for:

less

CopyEdit

S → AS | b

A → SA | a

FIRST:

o FIRST(S) = {a, b}

o FIRST(A) = {a, b}

FOLLOW:

o FOLLOW(S) = {$, a, b}

o FOLLOW(A) = {a, b}
4. State the concepts of Predictive parsing.

o Predictive parsing uses lookahead symbols to make parsing decisions.

o It is a top-down parser that does not backtrack and uses FIRST and FOLLOW
sets.

5. Differentiate Top Down parsing and Bottom Up parsing.

o Top-down: Starts from the start symbol and derives input.

o Bottom-up: Starts from input and reduces it to the start symbol.

6. Define Recursive Descent Parsing.

o A top-down parsing technique where each non-terminal has a recursive


function.

o It is simple but may not handle left recursion.

7. State the different error recovery methods of predictive parsing.

o Panic mode

o Phrase level recovery

o Error productions

o Global correction

8. Write an algorithm for finding FOLLOW.

scss

CopyEdit

1. Place $ in FOLLOW(S) where S is the start symbol

2. For A → αBβ, add FIRST(β) (except ε) to FOLLOW(B)

3. If β ⇒ ε or A → αB, then add FOLLOW(A) to FOLLOW(B)

4. Repeat until no more changes


9. What is the main idea of Left factoring? Give an example.

o Removes common prefixes to make grammar suitable for predictive parsing.


Example:

less

CopyEdit

A → ab | ac → A → aB

B→b|c

10. Define LL(1) Grammar.

• A grammar is LL(1) if it can be parsed left-to-right (L), with leftmost derivation (L),
using 1 lookahead token.

11. Difference between ambiguous and unambiguous grammar.

• Ambiguous: A string has more than one parse tree.

• Unambiguous: Every valid string has a unique parse tree.

12. Define parser. Explain the advantages and disadvantages of LR parsing.

• A parser builds a parse tree from tokens.

• LR parsing:

o Advantages: Handles more grammars, detects errors early.

o Disadvantages: Complex to implement and table-driven.

13. Define Augmented Grammar with an example.

• An augmented grammar has a new start symbol.

• Example:

o Original: S → aS | b
o Augmented: S' → S

14. Evaluate the conflicts encountered while parsing.

• Shift-Reduce Conflict: Parser cannot decide whether to shift or reduce.

• Reduce-Reduce Conflict: Parser cannot decide which rule to reduce.

15. Point out the categories of shift-reduce parsing.

• Operator precedence parser

• SLR (Simple LR)

• LALR (Look-Ahead LR)

• Canonical LR

16. How to create an input and output translator with YACC.

• Define grammar and actions in YACC.

• Use LEX to tokenize input.

• Link both and compile to get the translator.

17. Give the four possible actions of LR Parsing.

• Shift: Push token onto stack

• Reduce: Apply grammar rule

• Accept: Parsing successful

• Error: Invalid input

18. Solve if the grammar is ambiguous:

nginx

CopyEdit
S → aSbS | bSaS | ε

• Yes, it's ambiguous.

• Example: abab has multiple parse trees.

19. Discuss when dangling references occur.

• Occurs when a variable refers to memory that has been deallocated or changed.

• Common in dynamic memory usage.

20. Illustrate various types of recursion with example.

• Direct: A → Aα

• Indirect: A → B, B → Aα

• Tail: A → αA | ε

• Example: A → A + B | B (direct recursion)

21. Give the comparison between various LR parsers.


| Parser | Table Size | Power | Use |
|--------|------------|-------|-----|
| SLR | Small | Low | Simple grammars
| LALR | Medium | Medium| Most used
| LR(1) | Large | High | All deterministic grammars

22. Write down the structure of YACC file.

shell

CopyEdit

%{ Declarations %}

%%

Grammar rules
%%

Supporting C code

23. Differentiate Lex and YACC.

• Lex: Token generator (lexer)

• YACC: Grammar parser (parser generator)

• Lex handles lexical rules; YACC handles syntax rules.

24. Write about Closure Operation.

• Closure(I) adds items to a set of LR(0) items by exploring productions of non-


terminals after a dot (.).

• Used in constructing parsing tables.



1. List out the two rules for type checking.
o Type Compatibility Rule: Operands in an expression must be compatible.
o Type Conversion Rule: Implicit or explicit conversion must be valid.

2. Compare synthesized attributes and inherited attributes.


Synthesized Inherited

Computed from child nodes Computed from parent or siblings

Flow bottom-up Flow top-down or sideways

3. What is Annotated Parse Tree?


o A parse tree with attribute values at each node, based on semantic rules.

4. Define Type Checker.


o A compiler component that ensures semantic correctness by verifying that
operand types match expected types.

5. What is a syntax tree?


o A hierarchical tree structure representing the syntactic structure of code.
o For: a := b * -c + b * -c
r
CopyEdit
:=
/ \
a +
/\
* *
/\ /\
b -c b -c

6. Define type systems.


o A collection of rules assigning types to variables and expressions to ensure
program correctness.

7. Express the rule for checking the type of a function.


o The type of a function is correct if the types of actual parameters match the
formal parameter types, and the return type is compatible with the
function's declared return type.

8. Define Syntax Directed Definition (SDD) of a simple desk calculator.


kotlin
CopyEdit
E → E1 + T { E.val = E1.val + T.val }
E→T { E.val = T.val }
T → num { T.val = num.lexval }

9. Identify the different types of intermediate representation.


o Three-address code (TAC)
o Syntax trees
o Postfix notation
o Directed Acyclic Graphs (DAGs)

10. Difference between syntax-directed definitions and translation schemes.


• SDD: Uses attributes with rules only.
• Translation Scheme: Embeds semantic actions directly in grammar rules.
11. State the type expressions.
• Describes the types of language constructs. Examples:
o int, float, array(int), pointer(float), record {name:string, age:int}

12. Illustrate the methods of implementing three-address statements.


• Quadruples: op, arg1, arg2, result
• Triples: op, arg1, arg2 (no explicit result)
• Indirect triples: Array of pointers to triples

13. Differentiate S-attribute and L-attribute definitions.


• S-attributed: Only synthesized attributes
• L-attributed: Includes synthesized and inherited attributes with left-to-right
evaluation

14. Create postfix notation for the expression a + b * c.


• abc*+

15. Translate conditional statement if a < b then 1 else 0 into three-address code.
go
CopyEdit
if a < b goto L1
t := 0
goto L2
L1: t := 1
L2:

16. Test if the rules are L-attributed:


ini
CopyEdit
A.s = B.b
B.i = f(C.c, A.s)
• Yes, L-attributed:
o A.s is synthesized from B.b,
o B.i is inherited from A.s (parent), and C.c (sibling to right, not allowed in L-
attributed).
o Since it uses right-sibling’s attributes, not L-attributed.

17. What are the methods of representing a syntax tree?


• Pointer-based trees
• Array representation
• Linearized structure (Postfix/Prefix notation)

18. Construct the syntax directed definition for if-else statement:


csharp
CopyEdit
S → if E then S1 else S2
{
S.code = E.code || 'if E goto L1 else L2' || L1 || S1.code || goto L3 || L2 || S2.code || L3
}

19. Examine the usage of syntax-directed definition.


• Specifies translation actions tied to grammar rules.
• Used for type checking, intermediate code generation, and syntax tree construction.

20. Show three-address code for d = (a - b) + (a - c) + (a - c)


ini
CopyEdit
t1 = a - b
t2 = a - c
t3 = t1 + t2
t4 = a - c
t5 = t3 + t4
d = t5

21. Give the evaluation order of an SDD.


• Evaluate attributes based on dependency graph.
• Topological order ensures that dependencies are resolved before evaluation.

22. What is a translation scheme?


• A context-free grammar with embedded semantic actions (code snippets).

23. How will you evaluate semantic rules?


• Build a dependency graph and use topological sort to evaluate in correct order.

24. Illustrate how to construct syntax tree for an expression.


• For a + b * c:
css
CopyEdit
+
/\
a *
/\
b c

1. What is heap allocation?


o Heap allocation allows dynamic memory allocation at runtime. Memory is
managed using pointers and released manually (e.g., using free() in C).

2. How is the activation record pushed onto the stack?


o During a function call, the activation record is created and pushed onto the
runtime stack. It includes parameters, return address, and local variables.

3. Analyze the storage allocation strategies.


o Static Allocation: Fixed memory at compile time.
o Stack Allocation: Memory managed via stack for recursive calls.
o Heap Allocation: For dynamic, variable-lifetime data.

4. State the principles for designing calling sequences.


o Minimize overhead
o Preserve registers
o Maintain calling convention
o Optimize parameter passing

5. List out the dynamic storage techniques.


o Heap Allocation
o Garbage Collection
o Memory Pooling
o Buddy System

6. Define the non-local data on stack.


o Non-local data refers to variables not declared in the current procedure but
accessible due to nesting (e.g., global or in enclosing scopes).
7. Define variable data length on the stack.
o When functions have dynamic-size local data (e.g., arrays), their sizes are
determined at runtime and stored within the activation record.

8. Differentiate between stack and heap allocation


Stack Allocation Heap Allocation

Managed automatically Managed manually

Faster access Slower access

Limited in size Larger size

Supports recursion Supports dynamic structures

9. Distinguish between static and dynamic storage allocation.


Static Dynamic

Allocated at compile-time Allocated at runtime

Fixed size Variable size

Efficient but inflexible Flexible but may have overhead

10. Discuss the main idea of Activation Tree.


• Represents the dynamic calls of procedures during execution. Parent-child
relationship shows calling structure.

11. Give the fields in an Activation Record.


• Return address
• Parameters
• Local variables
• Control link
• Access link
• Temporary data

12. Compose space efficiency and program efficiency.


• Space Efficiency: Uses minimal memory resources.
• Program Efficiency: Balances memory and CPU usage for better performance.
13. Construct typical memory hierarchy configuration of a computer.
css
CopyEdit
Registers → Cache → Main Memory (RAM) → SSD/HDD → Backup Storage

14. How would you solve issues in code generator design?


• Use intermediate code optimization
• Apply instruction selection and scheduling
• Handle register allocation smartly
• Use machine-specific information

15. Evaluate Best-fit and Next-fit object placement.


• Best-fit: Finds the smallest hole that fits. Reduces fragmentation but slower.
• Next-fit: Starts from the last allocation point. Faster but may cause fragmentation.

16. Prepare optimal code sequence for the given sequence:


ini
CopyEdit
t=a+b
t=t*c
t=t/d
• Optimized Code:
ini
CopyEdit
t1 = a + b
t2 = t1 * c
t3 = t2 / d
t = t3

17. Analyze different forms of machine instructions.


• Register-register
• Register-memory
• Immediate
• Stack-based
• Accumulator-based

18. Discuss the four principle uses of registers in code generation.


• Holding operands
• Temporary storage
• Holding addresses
• Storing return values

19. Examine what is the input to code generator.


• Intermediate representation (like three-address code), symbol table, and target
machine details.

20. Advantages and disadvantages of register allocation and assignment?


• Advantages: Speeds up execution, reduces memory access.
• Disadvantages: Complex to manage with limited registers, potential spilling.

21. How the use of registers is subdivided into two sub-problems?


• Register Allocation: Decide which variables to keep in registers.
• Register Assignment: Assign specific registers to variables.

22. Organize the contents of activation record.


• Parameters
• Return address
• Local variables
• Temporary values
• Control and access links

1. Examples of function preserving transformations:
o Common subexpression elimination
o Constant folding
o Dead code elimination
o Loop unrolling
o Strength reduction

2. Concept of copy propagation:


o Replaces the use of a variable with its assigned value.
o Example: If x = y, then later uses of x can be replaced by y.

3. Use of machine idioms:


o Machine idioms replace intermediate code with efficient target machine
instructions using known instruction patterns.
4. Flow graph for quicksort algorithm:
o A control flow graph showing function calls, recursive branches
(partitioning), and base cases. Nodes = blocks, Edges = control flow.

5. Constructs for optimization in basic block:


o Elimination of common subexpressions
o Dead code elimination
o Constant folding
o Copy propagation
o Strength reduction

6. Properties of optimizing compilers:


o Preserve program semantics
o Improve performance
o Target-independent optimizations
o Minimize resource usage
o Apply safe transformations

7. Define data flow analysis:


o Technique to gather information about the possible set of values calculated
at various points in a program.

8. Liveness of a variable:
o A variable is live at a point if its value is used in the future.
o Calculated by backward analysis using use-def chains.

9. What is DAG? Advantages of DAG:


o DAG (Directed Acyclic Graph): Represents expressions in basic blocks.
o Advantages: Eliminates redundant computations, aids in code optimization.

10. Uses of Gen and Kill functions:


• Gen: Variables defined or generated in a block.
• Kill: Variables whose values are overwritten.
• Used in data flow analysis.

11. Basic blocks and flow graphs:


• Basic block: A sequence of statements with one entry and one exit.
• Flow graph: Nodes = basic blocks, edges = control flow.

12. Main idea of constant folding:


• Evaluates constant expressions at compile-time.
• Example: x = 4 + 5 → x = 9

13. Three address code for: d := (a - b) + (a - c) + (a - c)


ini
CopyEdit
t1 = a - b
t2 = a - c
t3 = t1 + t2
t4 = t3 + t2
d = t4

14. DAG for basic block:


go
CopyEdit
d := b * c
e := a + b
b := b * c
a := e - d
• DAG merges repeated sub-expressions like b * c.

15. Role of target machine in code generation:


• Determines instruction set, register availability, addressing modes, and affects
instruction selection and scheduling.

16. DAG for: a = (a * b + c) - (a * b + c)


• All expressions cancel out, so optimized code: a = 0

17. Code for x = a / (b + c) - d * (e + f) (3 registers):


ini
CopyEdit
t1 = b + c
t2 = a / t1
t3 = e + f
t4 = d * t3
x = t2 - t4

18. Characteristics of peephole optimization:


• Local, small-scale optimizations
• Improves performance by removing inefficiencies
• Examples: eliminating redundant loads, replacing slow instructions

19. Algebraic transformations:


• Simplify expressions using algebraic laws
• Example: x = x * 2 → x = x + x

20. What is a flow graph?


• A directed graph where nodes represent basic blocks and edges show control flow
between them.

21. Dead code elimination (with example):


• Removes code that doesn’t affect the program’s output.
• Example:
ini
CopyEdit
x=5
x = 6 ← x=5 is dead

22. Example for code motion:


• Moving invariant code out of loops
• Example:
arduino
CopyEdit
for i in 1 to n:
x = a + b ← move this outside the loop

23. Strength reduction in code optimization:


• Replace expensive operations with cheaper ones
• Example: x = y * 2 → x = y + y

Finish he he he !!!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy