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

CD Exp-5 Material

The document outlines the construction of an LL(1) parser for arithmetic expressions, detailing the algorithm for generating a parsing table and the requirements for implementing the parser. It includes a C program that demonstrates the parsing process, handling expressions and providing feedback on parsing success or failure. Key concepts such as FIRST and FOLLOW sets, as well as the handling of grammar ambiguities, are discussed.

Uploaded by

yuvaraj250921
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 views4 pages

CD Exp-5 Material

The document outlines the construction of an LL(1) parser for arithmetic expressions, detailing the algorithm for generating a parsing table and the requirements for implementing the parser. It includes a C program that demonstrates the parsing process, handling expressions and providing feedback on parsing success or failure. Key concepts such as FIRST and FOLLOW sets, as well as the handling of grammar ambiguities, are discussed.

Uploaded by

yuvaraj250921
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/ 4

Roll No:

EXERCISE – 5

Aim: Construct a LL (1) parser for an expression.


Description:
Algorithm can be applied to any grammar G to produce a parsing table M. For some Grammars, for example
if G is left recursive or ambiguous, then M will have at least one multiply-defined entry. A grammar whose
parsing table has no multiply defined entries is said to be LL (1). It can be shown that the above algorithm
can be used to produce for every LL (1) grammar G a parsing table M that parses all and only the sentences
of G. LL (1) grammars have several distinctive properties. No ambiguous or left recursive grammar can be
LL (1). There remains a question of what should be done in case of multiply defined entries. One easy
solution is to eliminate all left recursion and left factoring, hoping to produce a grammar which will produce
no multiply defined entries in the parse tables. Unfortunately, there are some grammars which will give an
LL (1) grammar after any kind of alteration. In general, there are no universal rules to convert multiply
defined entries into single valued entries without affecting the language recognized by the parser.
The prime requirements are: -
→ Stack
→ Parsing Table
→ Input buffer
→ Parsing program
Algorithm:
1. Compute FIRST(X) as follows:
→ if X is a terminal, then FIRST(X)={X}.
→ if X→ is a production, then add  to FIRST(X).
→ if X is a non-terminal and X→Y1Y2...Yn is a production, add FIRST(Yi) to FIRST(X) if the
preceding Yjs contain  in their FIRSTs.
2. Compute FOLLOW as follows:
→ FOLLOW(S) contains $, If S is the starting symbol of the grammar.
→ For productions A→B, everything in FIRST () except  goes into FOLLOW(B).
→ For productions A→B or A→B where FIRST () contains , FOLLOW(B) contains everything
that is in FOLLOW(A).
3. For each production A→ do:
→ For each terminal a  FIRST () add A→ to entry M [A, a].
→ If FIRST (), add A→ to entry M [A, b] for each terminal b  FOLLOW(A).
→ If FIRST () and $FOLLOW(A), add A→ to M[A,$].
4. If there is two or more number of productions in any cell then grammar is not LL (1).

Program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

20 | P a g e
Roll No:

#define MAX_EXPR_LENGTH 100


void E();
void EPrime();
void T();
void TPrime();
void F();
void match(char);
char expr[MAX_EXPR_LENGTH];
int index = 0;
char lookahead;
int main() {
printf("Enter an arithmetic expression: ");
fgets(expr, sizeof(expr), stdin);
lookahead = expr[index];
E();
if (lookahead == '\0' || lookahead == '\n') {
printf("Parsing successful.\n");
} else {
printf("Parsing failed.\n");
}
return 0;
}
void E() {
printf("E -> TE'\n");
T();
EPrime();
}
void EPrime() {

if (lookahead == '+') {
printf("E' -> +TE'\n");
match('+');
T();
EPrime();

21 | P a g e
Roll No:

} else {
printf("E' -> e\n");
}
}
void T() {
printf("T -> FT'\n");
F();
TPrime();
}
void TPrime() {
if (lookahead == '*') {
printf("T' -> *FT'\n");
match('*');
F();
TPrime();
} else {
printf("T' -> e\n");
}
}
void F() {
if (isdigit(lookahead)) {
printf("F -> %c\n", lookahead);
match(lookahead);
} else if (lookahead == '(') {
printf("F -> (E)\n");
match('(');
E();
match(')');
} else {
printf("Parsing failed: Unexpected character '%c'\n", lookahead);
exit(1);
}
}
void match(char expected) {

22 | P a g e
Roll No:

if (lookahead == expected) {
if (expected != '\0') {
index++;
lookahead = expr[index];
}
} else {
printf("Parsing failed: Expected '%c' but found '%c'\n", expected, lookahead);
exit(1);
}
}

Output:
Enter an arithmetic expression: 3*1
E -> TE'
T -> FT'
F -> 3
T' -> *FT'
F -> 1
T' -> e
E' -> e
Parsing successful.

23 | P a g e

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