answersLogoWhite

0

Aim:

Parse the following grammar.

E --> E + T / T

T --> T * F / F

F --> ( E ) / id

Program:

#include

#include

#include

#include

void err(const char* s)

{

perror(s);

exit(0);

}

int factor()

{

int val,i;

char ch[0];

scanf("%s",ch);

switch(ch[0])

{

case '(':

val=expr();

scanf("%s",ch);

if(ch[0]!=')')

err("Missing closing paranthesis in factor.");

break;

default :{

for(i=0;i

{

if((ch[i]>'0')&&(ch[i]<='9'))

continue;

else

err("Illegal character sequence in place of factor.");

}

val=atoi(ch);

}

}

return val;

}

int term()

{

int val;

char ch[10];

val=factor();

while(1)

{

scanf("%s",ch);

if(ch[0]=='*')

{

val=val*factor();

}

else

break;

}

ungetc(ch[0],stdin);

return val;

}

int expr()

{

int val;

char ch[10];

val=term();

while(1)

{

scanf("%s",ch);

if(ch[0]=='+')

val=val+term();

else

break;

}

ungetc(ch[0],stdin);

return val;

}

main()

{

printf("\nEnter the expression: ");

printf("\n Result: %d\n",expr());

}

Output:

nn@linuxmint ~ $ gcc rec.c

nn@linuxmint ~ $ ./a.out

Enter the expression: 5 * ( 3 + 1 )

;

Result: 20

nn@linuxmint ~ $

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Why you are eliminating left recursion?

Left recursion is eliminated in grammar to prevent infinite loops during parsing in top-down parsing techniques, such as recursive descent parsers. When a grammar has left recursion, it can lead to non-termination as the parser continually tries to expand the leftmost non-terminal. By transforming left-recursive rules into right-recursive ones, we ensure that the parser can eventually produce a string and terminate successfully. This transformation enhances the parser's efficiency and correctness.


What is lex tool?

Lex is a parser generator tool. Its basically a program that generates the lexers.


How do you parse XML using JavaScript?

To manipulate an XML document in javascript, you need an XML parser. Today all browsers come with in-built parsers that can parse the XML document. The parser loads the document into your computer's memory. Once the document is loaded, its data can be manipulated using the DOM(Document Object Model). There is significant differences in implementation of Microsoft Browser based XML parser and the Mozilla browsers based XML parser.


How do you generate a parse tree from an expression using C program?

c code for top down parser


What is a parser?

Parser is a portion of the computer progam which carry out parsing process.


When was Pdf-parser created?

Pdf-parser was created on 2008-05-02.


A top down parser generates?

a top down parser generates a leftmost derivation


Why is XML Parser used in modern browsers?

XML Parser is used to decipher XML codes. Without it, the codes cannot be understood. Web browsers, such as Mozilla Firefox and Google Chrome supply the user with this XML Parser.


Where can you find an assembly program compiler and editor for the TI 84 calculator?

There is a TI asm manual with links on ticalc.org. But before you try that, Google "Axe Parser". Its not as good as it looks-- its better!


Do you need MSXML parser?

yes i need


What checks for structural errors of a language?

parser


What is an email parser?

An email parser is a technology that allows you to integrate your campaign with these and other services so that when a transaction is made with that service, a subscriber can be added to your campaign.