CM 2 Mark
CM 2 Mark
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.
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.
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
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}
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).
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.
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:
Input: 3+53 + 5
[E, value=8]
|
[E, value=3] [+] [T, value=5]
|
[T, value=3]
In this tree, each node is annotated with its computed semantic value.
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.
S>a|^|(R)
T>T*F|F
F>(E)|id
Grammar:
FIRST Sets:
FOLLOW Sets:
FOLLOW(S) = {$}\{\$ \}
FOLLOW(T) = {),$}\{), \$\}
FOLLOW(F) = {),$}\{), \$\}
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(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:
Final Answer:
%{
#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;
}
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
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
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:
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.