Pcd Record

March 25, 2017 | Author: blacklistrivals711 | Category: N/A
Share Embed Donate


Short Description

Download Pcd Record...

Description

Ex. No:

IMPLEMENTATION OF LEXICAL ANALYSIS IN C

Date:

AIM: To write a C Program to implement a Lexical analyzer.

ALGORITHM: 1) Start the program.

2) Declare all the variables and file pointers. 3) Display the input program. 4) Separate the keyword in the program and display it. 5) Display the header files of the input program. 6) Separate the operators of the input program and display it. 7) Print the punctuation marks. 8) Print the constant that are present in input program. 9) Print the identifiers of the input program. 10)

Stop the program.

PROGRAM CODING: #include #include #include #include void main() { FILE *fp; int i,j; char arr[100],k; char kw[10][10]={"int","float","double","end","main","void","include","printf","scanf"}; char hf[2][10]={"stdio.h","conio.h"}; char op[5]={'+','-','*','/','%'};

char punc[6]={'(',')','{','}',','}; clrscr(); fp=fopen("input.c","r"); printf("Input Program\n"); while(!feof(fp)) { arr[0]=fgetc(fp); printf("%c",arr[0]); } fclose(fp); printf("\nSymbol table\n"); fp=fopen("input.c","r"); printf("\nKeywords"); while(!feof(fp)) { arr[0]=fgetc(fp);

fscanf(fp,"%s",arr); for(i=0;i 1) { FILE *file; file=fopen(argv[1],"r"); if(!file) { printf("could not open %s\n",argv[1]); exit(0); } yyin=file; } yylex(); printf("\n\n"); return 0; } int yywrap() {

return 0; }

INPUT (area.c): #include #include double area_of_circle(double r); int main(int argc,char *argv[]) { if(argc < 2) { printf("usage: %s radius\n",argv[0]); { exit(1); }

else { double radius=atof(argv[1]); double area=area_of_circle(radius); printf("Area of circle with radius %f=%f\n",radius,area); } return 0; }

OUTPUT:

RESULT: Thus the above the program is executed and the required output is obtained.

Ex. No: Date:

IMPLEMENTATION OF CALCULATION USING LEX AND YACC

AIM: To write a program to implement calculator using lex and yacc.

ALGORITHM: 1. Start the program. 2. Perform the calculation using both the lex and yacc. 3. In the lex tool, if the given expression contains numbers and letters then they are

displayed. 4. In the same way, the digits, letters and uminus are identified and displayed using yacc

tool. 5. The calculation is performed and the result is displayed. 6. Stop the program.

PROGRAM CODING: USING LEX TOOL: %{ #include #include"y.tab.h" int c; extern int yylaval; %} %% ""; [a-z] { c=yytext[0]; yylaval=c-'a'; return(LETTER); } [0-9] { c=yytext[0]; yylaval=c-'0'; return(DIGIT); } [a-z0-9/b]{ c=yytext[0]; return(c); }

USING YACC TOOL: %{ #include int regs[26]; int base; %} %start list %token DIGIT LETTER %left'|' %left'&' %left'+''-' %left'*''/''%' %left UMINUS %% list: | list stat'\n' | list error'\n' { yyerror(); }; stat:expr { printf("%d\n",$1); } |

LETTER'='expr { regs[$1]=$3; }; expr:'('expr')' { $$=$2; } | expr'*'expr { $$=$1*$3; } | expr'/'expr { $$=$1/$3; } | expr'%'expr { $$=$1%$3; } | expr'+'expr { $$=$1+$3;

} | expr'-'expr { $$=$1-$3; } | expr'&'expr { $$=$1&$3; } | expr'|'expr { $$=$1|$3; } | '-'expr%prec UMINUS { $$=-$2; } | LETTER { $$=regs[$1]; } |

number ; number:DIGIT { $$=$1; base=($1==0)?8:10; } | number DIGIT { $$=base*$1+$2; } %% main() { return(yyparse()); } yyerror(s) char*s; { fprintf(stderr,"%s\n",s); } yywrap() { return(1); }

OUTPUT:

RESULT: Thus the above the program is executed and the required output is obtained.

Ex. No: Date:

IMPLEMENTATION OF OPERATOR PRECEDENCE PARSER

AIM: To write a program to implement operator precedence parser.

ALGORITHM: 1. Start the program. 2. Set the ip to print the first symbol of ws. 3. Let x be the top of the stack pointed by the ip. 4. If $ is on the top of the stack and the ip point to $ then return else let a be the terminal on the stack and b be the symbol pointed by ip. 5. I ab then pop the stack until the top stack terminal is related by < to terminal most recentiy popped else error(); 7. Stop the program.

PROGRAM CODING: #include #include #include #include int scan(int); int number(char); int findingG(); int findingL(); int erase(char); char opers[6]={'i','+','*','$','/','-'},input[50]; char table[6] [6]={'=','>','>','>','>','>','','','>','>','','>','>','>','>','.%c",prod[0].left,prod[0].left); for(i=0;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF