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

EXNO3&4

The document describes implementing a recursive descent parser to parse arithmetic expressions. It includes functions for lexical analysis to tokenize the input into symbols and identifiers, and recursive functions for parsing expressions, terms, and factors according to the grammar rules of arithmetic expressions. An example program demonstrates parsing a simple addition expression and displaying the tokens.

Uploaded by

Anish Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
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)
35 views

EXNO3&4

The document describes implementing a recursive descent parser to parse arithmetic expressions. It includes functions for lexical analysis to tokenize the input into symbols and identifiers, and recursive functions for parsing expressions, terms, and factors according to the grammar rules of arithmetic expressions. An example program demonstrates parsing a simple addition expression and displaying the tokens.

Uploaded by

Anish Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 12

EXNO: 3

IMPLEMENTAION OF LEXICAL ANALYSER USING LEX TOOL

AIM:
To write LEX program to create a lexical analyzer for subset of C program.

ALGORITHM:
Step 1: Start the Program Step 2: Give the preprocessor directives to be indentified . Step 3: Give the keyword to be identified. Step 4: Indicate the begin and end of the block. Step 5: Check for string, number, assignment operator and relational operator. Step 6: Above function are accessed in main. Step 7: Area C program is given as the input to lex program in order to identify the tokens. Step 8: Run the program and get th output. Step 9: Stop the Process.

PROGRAM FOR LEXICAL ANALYSIS USING LEX TOOL

/*lexc.l*/ %{ int COMMENT=0; %} identifier[a-z|A-Z][a-z|A-Z|0-9]* %% #.* {printf("\n%s is a preprocesor directive",yytext);} int {printf("\n\t%s is a keyword",yytext);} float {printf("\n\t%s is a keyword",yytext);} double {printf("\n\t%s is a keyword",yytext);} char {printf("\n\t%s is a keyword",yytext);} if {printf("\n\t%s is a keyword",yytext);} else {printf("\n\t%s is a keyword",yytext);} while {printf("\n\t%s is a keyword",yytext);} do {printf("\n\t%s is a keyword",yytext);} return {printf("\n\t%s is a keyword",yytext);} break {printf("\n\t%s is a keyword",yytext);} continue {printf("\n\t%s is a keyword",yytext);} void {printf("\n\t%s is a keyword",yytext);} switch {printf("\n\t%s is keyword",yytext);} for {printf("\n\t%s is a keyword",yytext);} typedef {printf("\n\t%s is a keyword",yytext);} struct {printf("\n\t%s is a keyword",yytext);} goto {printf("\n\t%s is a keyword",yytext);} "/*" {COMMENT=1;} "*/" {COMMENT=0;} {identifier}\( {if(!COMMENT) printf("\nFUNCTIONS\n\t%s",yytext);} \{ {if(!COMMENT) printf("\nBLOCK BEGINS");} \} {if(!COMMENT) printf("\nBLOCK ENDS");} {identifier} {if(!COMMENT) printf("\n%sIDENTIFIER",yytext);} {identifier}(\[[0-9]*\])?\( {if(!COMMENT) printf("\n%sIDENTIFIER",yytext);} \".*\" {if(COMMENT)printf("\n\t%s is a string",yytext);} [0-9]+ {if(COMMENT)printf("\n\t%s is a number",yytext);} \)(\;)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");} \(ECHO; = {if(!COMMENT) printf("\n\t%s is an assignment operator",yytext);} \> {if(!COMMENT) printf("n\t%s is a relational operator",yytext);} \\n %% int main(int argc,char **argv) { if(argc>1) { FILE *file; file=fopen(argv[1],"r");

if(!file) { printf("COULD NOT OPEN %s\n",argv[1]); exit(1); } yyin=file; } yylex(); printf("\n\n"); return 0; } int yywrap() { return 0; }

INPUT /*x.c*/ #include<stdio.h> #include<conio.h> void main() { int a ; printf( Enter the value of a : ) ; scanf( %d ,&a) ; getch() ; }

OUTPUT [cse@localhost cd]$ lex lex.l [cse@ localhost cd]$ cc lex.yy.c [cse@ localhost cd ]$ ./a.out x.c #include<stdio.h> is a prepocessor directive #include<conio.h> is a prepocessor directive void is a keyword main is a function() BLOCK BEGINS int is a keyword aIDENTIFIER, Printf is a function ( is a string Enter is a function The is a function a is a function : is a string) ; scanf is a function( is a string% d is a function is a string,& a is a function ) ; getch is a function () ;

BLOCK ENDS

EXNO: 4

IMPLEMENTAION OF RECURSIVE DESCENT PARSING

AIM:
To write a program for implementing recursive parser and verify with the arithmetic expression.

ALGORITHM:
Step 1: Start the Program Step 2:Read the input statement or expression.. Step 3: Indentify the token form the expression and store it in symbol table Step 4:Read the first token from symbol table and display the output. Step 5: Recursively call the function in order to find other similar token in that given expression till you reach semicolon; Step 6: Read the next token and repeat the step 5. Step 7: Stop the Process

Program #include<stdio.h> #include<conio.h> #include<ctype.h> #define SIZE 128 #define NONE -1 #define EOS '\0' #define KEYWORD 257 #define BRACES 254 #define PAREN 258 #define NUM 256 #define ID 259

#define ASSIGN 260 #define RE_LOP 261 #define DONE '.' #define MAX 100 #define INC 250 #define DCR 250 #define COMMA 252 char lexemes[MAX]; char buffer[SIZE]; int lastchar=-1; int lastentry=0; int tokenval=NONE; int lineno=1; int lookahead; struct entry { char *lexptr; int token; }symtable[100]; struct entry keywords[]={"if",KEYWORD,"int",KEYWORD,"for",KEYWORD,"float",KEYWORD,"doubl e",KEYWORD,"char",KEYWORD,"struct",KEYWORD,"return",KEYWORD,"main",KEYWO RD,"else",KEYWORD,0,0}; //function to display error void error_message(char *m) { printf("line%d:%s\n",lineno,m); exit(1); } // lookup function int look_up(char s[]) { int k; for(k=lastentry;k>0;k=k-1) if(strcmp(symtable[k].lexptr,s)==0) return k; return 0; } // function to insert keywords and identifier in symbol table int insert(char s[],int tok) { int len;

len = strlen(s); if(lastentry+1>=MAX) error_message("symbol table is full"); lastentry=lastentry+1; symtable[lastentry].token=tok; symtable[lastentry].lexptr=&lexemes[lastchar+1]; lastchar=lastchar+len+1; strcpy(symtable[lastentry].lexptr,s); return lastentry; } // function to intialize the symbol table void initialize() { struct entry *ptr; for(ptr=keywords;ptr->token;ptr++) insert(ptr->lexptr,ptr->token); } // function to check for tokens int lexer() { int t; int val,i=0; while(1) { t=getchar(); if(t==' '||t=='\t'); else if(t=='\n') lineno = lineno + 1; else if(isdigit(t)) { ungetc(t,stdin); scanf("%d",&tokenval); return NUM; } else if(t==';') return t; else if(isalpha(t)) { while(isalnum(t)) {

buffer[i]=t; t=getchar(); i=i+1; if(i>SIZE) error_message("complier error"); } buffer[i]=EOS; if(t!=EOF) ungetc(t,stdin); val=look_up(buffer); if(val==0) val=insert(buffer,ID); tokenval=val; return symtable[val].token; } else { if(t=='.') return DONE; tokenval=NONE; return t; } } } void match(int t) { if(lookahead==t) lookahead=lexer(); else error_message("syntax error"); } void display(int t, int tval) { if(t=='+'||t=='-'||t=='*'||t=='/') printf("\n Arithmetic operator=%c\t",t); else if(t==NUM) printf("\n Number:%d\n",tval); else if(t==ID) printf("\n Identifier:%s\n",symtable[tval].lexptr); else printf("\nToken %dTokenval%d\n",t,tokenval); } void F() {

void E(); switch(lookahead) { case '(': { match('('); E(); match(')'); break; } case NUM: { display(NUM,tokenval); match(NUM); break; } case ID: { display(ID,tokenval); match(ID); break; } default:error_message("Syntax error"); } } void T() { int t; F(); while(1) { switch(lookahead) { case '*': { t=lookahead; match(lookahead); F(); display(t,NONE); continue; } case'/': { t=lookahead; match(lookahead); F(); display(t,NONE); continue;

} default: return; } } } void E() { int t; T(); while(1) { switch(lookahead) { case '+': { t=lookahead; match(lookahead); T(); display(t,NONE); continue; } case '-': { t=lookahead; match(lookahead); T(); display(t,NONE); continue; } default: return; } } } void parser() { lookahead=lexer(); while(lookahead!='.') { E(); match(';'); } } void main() { char ans; clrscr();

printf("\n\n\t\tPROGRAM FOR RECURSIVE DESCENT PARSING\n\n"); initialize(); printf("\nEnter the expression and place; at the end and place . to end:\n\n"); parser(); //lookahead=lexer(); //getch(); }

Oupput INPUT Enter the expression and place; at the end and place . to end: a+b; OUPUT Identifier:a Identifier:b Arithmetic operator : +

You might also like