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

ACD key points

key points

Uploaded by

devi.g
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

ACD key points

key points

Uploaded by

devi.g
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1. Difference between a compiler and an interpreter?

Interpreter Compiler

Scans the entire program and


Translates program one statement at a
translates it as a whole into machine
time.
code.

Interpreters usually take less amount of Compilers usually take a large amount
time to analyze the source code. of time to analyze the source code.
However, the overall execution time is However, the overall execution time is
comparatively slower than compilers. comparatively faster than interpreters.

Generates Object Code which further


No Object Code is generated, hence
requires linking, hence requires more
are memory efficient.
memory.

Programming languages like


Programming languages like C, C++,
JavaScript, Python, Ruby use
Java use compilers.
interpreters.

2. The rules for FIRST and FOLLOW

First and Follow sets are needed so that the parser can properly apply the needed production
rule at the correct position.

First(α) is a set of terminal symbols that begin in strings derived from α.

Rules For Calculating First Function-


Rule-01:

For a production rule X → ∈, First(X) = { ∈ }

Rule-02:

For any terminal symbol ‘a’, First(a) = { a }

Rule-03:
For a production rule X → Y1Y2Y3,
Calculating First(X)
If ∈ ∉ First(Y1), then First(X) = First(Y1)
If ∈ ∈ First(Y1), then First(X) = { First(Y1) – ∈ } ∪ First(Y2Y3)

Calculating First(Y2Y3)
If ∈ ∉ First(Y2), then First(Y2Y3) = First(Y2)
If ∈ ∈ First(Y2), then First(Y2Y3) = { First(Y2) – ∈ } ∪ First(Y3)
Similarly, we can make expansion for any production rule X → Y1Y2Y3…..Yn.

Follow Function-
Follow(α) is a set of terminal symbols that appear immediately to the right of α.

Rules For Calculating Follow Function-


Rule-01:

For the start symbol S, place $ in Follow(S).

Rule-02:

For any production rule A → αB,


Follow (B) = Follow (A)

Rule-03:

For any production rule A → αBβ,


If ∈ ∉ First(β), then Follow(B) = First(β)
If ∈ ∈ First(β), then Follow(B) = { First(β) – ∈ } ∪ Follow(A)

Calculate the first and follow functions for the given grammar-
S → aBDh
B → cC
C → bC / ∈
D → EF
E→g/∈
F→f/∈

First Functions-

First(S) = { a }

First(B) = { c }

First(C) = { b , ∈ }

First(D) = { First(E) – ∈ } ∪ First(F) = { g , f , ∈ }

First(E) = { g , ∈ }

First(F) = { f , ∈ }

Follow Functions-

Follow(S) = { $ }
Follow(B) = { First(D) – ∈ } ∪ First(h) = { g , f , h }
Follow(C) = Follow(B) = { g , f , h }
Follow(D) = First(h) = { h }
Follow(E) = { First(F) – ∈ } ∪ Follow(D) = { f , h }
Follow(F) = Follow(D) = { h }
3.Differentiate between SLR, LALR and CLR parsers

SLR Parser LALR Parser CLR Parser

It is very easy and cheap It is also easy and cheap to It is expensive and
to implement. implement. difficult to implement.

LALR and SLR have the CLR Parser is the largest.


SLR Parser is the smallest
same size. As they have less As the number of states
in size.
number of states. is very large.

Error detection can be


Error detection is not Error detection is not
done immediately in CLR
immediate in SLR. immediate in LALR.
Parser.

SLR fails to produce a It is intermediate in power It is very powerful and


parsing table for a certain between SLR and CLR i.e., works on a large class of
class of grammars. SLR ≤ LALR ≤ CLR. grammar.

It also requires more


It requires less time and It requires more time and
time and space
space complexity. space complexity.
complexity.

4. The push down automata accepts the input string in form of

1. Final State
2. Empty Stack

5. A PDA machine configuration (q, a, X) can be correctly represented as:

(Current state, unprocessed input, Stack content)

6. The value of n if Turing machine is defined using

n-tuples

A)7
7. Single-pass Compiler :

Advantage:
More effective than multi-pass compilers in the compiler point of
view.
Disadvantage:
It compiles less efficient programs.

8. the following error can a compiler check

a)Syntax Errors

9. Give the structure of Symbol table?

A symbol Table is an important data structure created and maintained


by the compiler in order to keep track of the semantics of variables
i.e. it stores information about the scope and binding information
about names, information about instances of various entities such as
variables and function names, classes, objects

10. How to left factor the grammar? Give Example.

To left factor a grammar, you can eliminate any common prefixes of symbols on
the right-hand side of the production for each nonterminal. Here's an example:
 Before left factoring: S ⇒ aX, S ⇒ aY, and S ⇒ aZ
After left factoring: S ⇒ aA', A' ⇒ AB / Bc / Ac
In this example, the productions S ⇒ aX, S ⇒ aY, and S ⇒ aZ have the same non-

terminal symbol on the left-hand side and a common prefix of a.


Left factoring is used to eliminate non-determinism in a grammar. It's also required
for a grammar to be suitable for top-down parsing.
Left recursion can create ambiguity and cause a grammar to create an infinite loop
during parsing. To eliminate left recursion, you can rewrite the offending
production.

Problem-01:

S → iEtS / iEtSeS / a
E→b

Solution-

The left factored grammar is-


S → iEtSS’ / a
S’ → eS / ∈
E→b

Problem-02:

Do left factoring in the following grammar-


S → bSSaaS / bSSaSb / bSb / a

Solution-

Step-01:

S → bSS’ / a
S’ → SaaS / SaSb / b
Again, this is a grammar with common prefixes.

Step-02:
S → bSS’ / a
S’ → SaA / b
A → aS / Sb
This is a left factored grammar.

You might also like