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

CM 2 Mark

The document covers various concepts in compiler design, including S-attributed grammars, LEX definitions, SLR vs. Canonical LR parsers, DAG constructions, and annotated parse trees. It also discusses syntax-directed definitions (SDD) and translations (SDT), along with examples of LEX programs and computations of FIRST, FOLLOW, LEADING, and TRAILING symbols. Additionally, it provides differences between synthesized and inherited attributes, and outlines the construction of DAGs for given expressions.

Uploaded by

omkarparande22
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)
3 views9 pages

CM 2 Mark

The document covers various concepts in compiler design, including S-attributed grammars, LEX definitions, SLR vs. Canonical LR parsers, DAG constructions, and annotated parse trees. It also discusses syntax-directed definitions (SDD) and translations (SDT), along with examples of LEX programs and computations of FIRST, FOLLOW, LEADING, and TRAILING symbols. Additionally, it provides differences between synthesized and inherited attributes, and outlines the construction of DAGs for given expressions.

Uploaded by

omkarparande22
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

1) Write a short note on s-attributed grammer

2) S-attributed Grammar:
3) An S-attributed grammar is a type of context-free grammar where all attributes are
synthesized. In this type of grammar, the values of an attribute for a non-terminal are
computed solely based on the values of the attributes of its children in the parse tree.
There are no inherited attributes in an S-attributed grammar.
4) In other words, for each non-terminal in the grammar, the synthesized attributes are
passed up the parse tree, starting from the leaves (terminals) up to the root. This
makes S-attributed grammars suitable for syntax-directed translation, where the
translation is performed as the parse tree is constructed.
5) Example:
For a simple arithmetic expression grammar:
6) E → E1 + T { E.val = E1.val + T.val }
7) E → T { E.val = T.val }
8) T → id { T.val = id.val }
9) In this example, the attribute val is synthesized for each non-terminal. The value of
E.val is computed based on the values of E1.val and T.val, and similarly for T.val.

2) write LEX definition for identifier.


Here is the LEX definition for an identifier:

[A-Za-z_][A-Za-z0-9_]* { return IDENTIFIER; }

This pattern matches strings that start with a letter (A-Z or a-z) or an underscore (_), followed
by any combination of letters, digits (0-9), or underscores. The action associated with this
pattern returns the token IDENTIFIER.

3) Differenciate between SLR and Cononical LR parser

1) SLR
1) Uses Follow sets to construct parsing tables.
2) Prone to conflicts due to simplistic handling of lookahead sets.
3) Fewer states, as it merges states with similar core items.
4) Fewer states, as it merges states with similar core items.
5) Smaller tables due to fewer states.
2) LR parser
1) Uses lookahead symbols explicitly defined in the states.
2) Less prone to conflicts due to precise use of lookahead symbols.
3) More states because it keeps different lookaheads in separate states.
4) Can handle a broader class of grammars, including those with more complex ambiguities.
5) Larger tables due to more states.
4) Construct the DAG for the following expression .
X+x*(x-y)+(y-z)*a

DAG Construction (Compact Form):

1. Leaf Nodes: X,x,y,z,aX, x, y, z, a


2. Intermediate Nodes:
o t1=x−yt1 = x - y
o t2=y−zt2 = y - z
o t3=x⋅t1t3 = x \cdot t1
o t4=t2⋅at4 = t2 \cdot a
o t5=X+t3t5 = X + t3
3. Root: t5+t4t5 + t4

This is the very compact representation of the DAG.

5) find first and follow of the following grammer.

S > Ad|B

A>aAB|b

B>bBa|E
First Sets:

 First(S): {a, b}
 First(A): {a, b}
 First(B): {b, ε}

Follow Sets:

 Follow(S): { $ }
 Follow(A): {d, b}
 Follow(B): { $, d, a, b}

6) Define SDD and SDT . State the task performed by SDT


SDD (Syntax Directed Definition):

 Definition: SDD specifies the attributes of grammar symbols and the rules (semantic
rules) for computing the values of these attributes.
 Key Features:
o Attributes can be synthesized (computed from children) or inherited
(computed from parents or siblings).
o Used for defining the semantics of a programming language.
SDT (Syntax Directed Translation):

 Definition: SDT is an extension of SDD where semantic actions are embedded into
the grammar rules. These actions can be executed during parsing to perform
translations.
 Key Features:
o Actions are written as snippets of code within grammar rules.
o Actions execute as the parser processes the input (e.g., constructing
intermediate representations or output).

Task Performed by SDT:

1. Translation: Converts input code into an intermediate form or target language.


2. Intermediate Code Generation: Generates intermediate representations such as
three-address code.
3. Error Detection: Implements semantic checks to identify and report errors.
4. Symbol Table Management: Updates and accesses the symbol table.
5. Optimization: Assists in optimizing the intermediate representation.
6. Code Emission: Outputs final machine or assembly code.

6) Construct DAG for the block

a=b+c

b=a-d

c=b+c

d=a-d
DAG Construction for the Block:

1. Nodes:
o t1=b+ct1 = b + c
o t2=a−dt2 = a - d
o t3=t2+ct3 = t2 + c (for c=b+cc = b + c, reuse bb as t2t2)
o t4=a−dt4 = a - d (reuse t2t2)
2. Edges:
o a→t1a \to t1
o b→t2b \to t2
o c→t3c \to t3
o d→t4d \to t4

Observations:
 b+cb + c, a−da - d, and c=b+cc = b + c reuse bb and dd where possible.
 t2t2 is reused for d=a−dd = a - d.

This ensures the DAG is compact and eliminates redundant computations.

7) Define Annotated Parse tree. Give an example.


Annotated Parse Tree:

An annotated parse tree is a parse tree in which each node is annotated with attribute values
computed using semantic rules. These attributes can be synthesized or inherited and are used
to represent the semantic information of the program.

Example:

For the grammar E→E+T ∣ TE \to E + T \ | \ T, with semantic rules for computing the value
of EE:

 E.value=E1.value+T.valueE.value = E1.value + T.value


 T.value=numT.value = num

Input: 3+53 + 5

Annotated Parse Tree:

[E, value=8]
|
[E, value=3] [+] [T, value=5]
|
[T, value=3]

In this tree, each node is annotated with its computed semantic value.

8) List and explain in short any two LEX library function.


1. yyinput()

 Description: Reads a single character from the input stream.


 Usage: It is used to fetch the next character during lexical analysis in LEX programs.
 Prototype: int yyinput(void);
 Explanation: This function can be redefined in LEX to customize how input is
processed (e.g., from a file or buffer).

2. yylex()
 Description: The main function in a LEX program that performs lexical analysis.
 Usage: It scans the input and returns the next token as per the defined regular
expressions.
 Prototype: int yylex(void);
 Explanation: It is automatically called to identify tokens from the input and is
responsible for tokenizing the input stream into meaningful chunks.

9) Calculate FIRST and follow for the following.

S>a|^|(R)

T>T*F|F

F>(E)|id
Grammar:

1. S→a ∣ ε ∣ (R)S \to a \ | \ \varepsilon \ | \ (R)


2. T→T⋅F ∣ FT \to T \cdot F \ | \ F
3. F→(E) ∣ idF \to (E) \ | \ id

FIRST Sets:

 FIRST(S) = {a,ε,(}\{a, \varepsilon, ( \}


 FIRST(T) = {(,id}\{ (, id \}
 FIRST(F) = {(,id}\{ (, id \}

FOLLOW Sets:

 FOLLOW(S) = {$}\{\$ \}
 FOLLOW(T) = {),$}\{), \$\}
 FOLLOW(F) = {),$}\{), \$\}

10) Give 2 differences between synthesized and inherited


attributes.
1. Direction of Flow:
o Synthesized Attributes flow upward from children to parents.
o Inherited Attributes flow downward from parents to children.
2. Computation:
o Synthesized Attributes depend on the attributes of the children nodes.
o Inherited Attributes depend on the attributes of the parent or sibling nodes.

11)Compute LEADING and TRAILING symbols of the


following grammar.
E>aA|SbB

A>aA|bB

B>b
To compute the LEADING and TRAILING symbols of the given grammar, follow these
steps:

Grammar:

1. E → E + T | T
2. T → T * F | F
3. F → (E) | id

LEADING Symbols (first symbol of derivations):

 LEADING(E): The first symbol of E can be derived from both E + T and T. So:
o From E + T, it can lead to LEADING(T).
o From T, it can lead to LEADING(T).
o LEADING(E) = LEADING(T).
 LEADING(T): The first symbol of T can be derived from T * F and F. So:
o From T * F, it can lead to LEADING(F).
o From F, it can directly lead to LEADING(F).
o LEADING(T) = LEADING(F).
 LEADING(F): The first symbol of F can be derived from either (E) or id. So:
o LEADING(F) = {(, id)}.

Thus:

 LEADING(E) = {(, id)}


 LEADING(T) = {(, id)}
 LEADING(F) = {(, id)}

TRAILING Symbols (last symbol of derivations):

 TRAILING(E): The trailing symbol of E can be derived from E + T and T. So:


o From E + T, it can lead to TRAILING(T).
o From T, it can lead to TRAILING(T).
o TRAILING(E) = TRAILING(T).
 TRAILING(T): The trailing symbol of T can be derived from T * F and F. So:
o From T * F, it can lead to TRAILING(F).
o From F, it can directly lead to TRAILING(F).
o TRAILING(T) = TRAILING(F).
 TRAILING(F): The trailing symbol of F can be derived from (E) or id. So:
o From (E), it leads to ).
o From id, it leads to id.
o TRAILING(F) = {), id}.
Thus:

 TRAILING(E) = {), id}


 TRAILING(T) = {), id}
 TRAILING(F) = {), id}

Final Answer:

 LEADING(E) = {(, id)}


 LEADING(T) = {(, id)}
 LEADING(F) = {(, id)}
 TRAILING(E) = {), id}
 TRAILING(T) = {), id}
 TRAILING(F) = {), id}

12) Write a LEX program to find sum of first n number


To create a LEX program that computes the sum of the first n numbers, you would typically
input the value of n and compute the sum of the integers from 1 to n. Here's a concise LEX
program to achieve this:

%{
#include <stdio.h>
int n, sum = 0;
%}

%%

[0-9]+ { n = atoi(yytext); }
\n {
sum = (n * (n + 1)) / 2; // Formula for sum of first n numbers
printf("Sum of first %d numbers is: %d\n", n, sum);
}
. { /* Ignore other characters */ }

%%

int main() {
printf("Enter a number n: ");
yylex(); // Start scanning input
return 0;
}

How this works:

1. Input: The user inputs a number n.


2. Computation: The program computes the sum of the first n natural numbers using
the formula sum = n * (n + 1) / 2.
3. Output: It prints the result.

This LEX program processes the input, calculates the sum, and outputs the result.
13) Construct DAG for the fpllowing expression

- b * (a+c)+ (a+c) * d

-i=i+5
To construct a Directed Acyclic Graph (DAG) for the given expressions:

1. Expression 1: - b * (a + c) + (a + c) * d
2. Expression 2: i = i + 5

We will break down the expressions and show their computation flow, reusing intermediate
results where applicable.

Expression 1: - b * (a + c) + (a + c) * d

1. (a + c) is computed once and reused.


o Create a node for a + c.

2. Multiplication:
o Compute b * (a + c).
o Compute (a + c) * d.

3. Addition:
o Add the results from the two multiplications: b * (a + c) + (a + c) * d.

4. Negation:
o Negate the sum: - (b * (a + c) + (a + c) * d).

Expression 2: i = i + 5

1. Addition:
o Add i + 5.

2. Assignment:
o Assign the result of the addition back to i.

DAG Construction

For Expression 1: - b * (a + c) + (a + c) * d

1. Create a node for a + c (this will be shared by both terms).


2. Create a node for b * (a + c).
3. Create a node for (a + c) * d.
4. Create a node for b * (a + c) + (a + c) * d.
5. Finally, create a node for the negation - (b * (a + c) + (a + c) * d).
For Expression 2: i = i + 5

1. Create a node for i + 5.


2. Create a node for the assignment i = i + 5.

DAG Representation
+-----+
| a+c | <--- (shared node)
+-----+
|
+-------------+ +-------------+
| b * (a+c) | | (a+c) * d |
+-------------+ +-------------+
| |
+---------------------------+
| b*(a+c) + (a+c)*d |
+---------------------------+
|
+--------------+
| - (result) |
+--------------+

i ------------> i + 5 ------------> i = i + 5

Explanation:

1. The subexpression (a + c) is computed once and used in both multiplications.


2. The result of b * (a + c) and (a + c) * d are added together, and then the result is
negated.
3. In the second expression, i + 5 is computed, and then the result is assigned back to i.

Key Points:

 The DAG shows a shared node for (a + c), which is reused in both b * (a + c) and (a
+ c) * d.
 The computations flow through a set of nodes representing the operations.
 The negation and assignment are the final steps in the DAG.

You might also like