compiler design lab manual

Upload: bernice-samina

Post on 29-Oct-2015

138 views

Category:

Documents


1 download

DESCRIPTION

containd programs and outputs of Compiler Design Lab.

TRANSCRIPT

EXNO 1TOKEN SEPARATION WITH EXPRESSION

Algorithm1.Include the nessary header files and declare the nessary variables.

2. Define the key words and the identifiers with the constant and operator.

3. Get the statement for analysis from the user.

4.Check each and every element in the statement with the defined keyword and if it matches print it as the keyword.

5.Check each and every element in the statement with the defined identifier and if it matches print the element as an identifier.

6 .Check each and every element in the statement with the defined constant then find the equalance and print it as constant.

7.Check each and every element in the statement with the defined operator and if it matches print the element as an identifier.

8.Display every element on the screen as separated.

9.Exit the program.

Program#include#include#include#includevoid main(){char exp[50]="\0",con[50]="\0",kwd[50]="\0",id[50]="\0",sym[50]="\0",opr[50]="\0";char key[6][10]={"if","for","do","while","int","float"};char ch;char ptr[10][10]={"\0"};int i=0,j=0,k=-1,n=-1,p=-1,s=-1;clrscr();puts("Enter the expression for lexical analysis");gets(exp);puts("\n\n\nThe tokens are");do{ch=exp[i];if(isalpha(ch)){k=-1;ptr[++n][++k]=ch;i++;ch=exp[i];if(isalpha(ch)||isdigit(ch)){while(isalpha(ch)||isdigit(ch)){ptr[n][++k]=ch;i++;ch=exp[i];}while(j+TE'|eT->FT'T'->*FT'|eF->(E)|id

Enter the Input String (use i for id): ( i + i ) * iSeq of production rulesE->TE'T->FT'F->(E)E->TE'T->FT'F->idT'->eE'->+TE'T->FT'F->idT'->eE'->eT'->*FT'F->idT'->eE'->e

EXNO 4B IMPLEMENTATION OF OPERATOR PRECEDENCE PARSER

Algorithm1. Include the nessary header files.2. Declare the nessary variables with the operators defined before.3. Get the input from the user and compare the string for any operators.4. Find the precedence of the operator in the expression from the predefined operator.5. Set the operator with the maximum precedence accordingly and give the relational operators for them.6. Parse the given expression with the operators and values.7. Display the parsed expression.8. Exit the program.

Program#include#include#include#include#includevoid main(){char stack[50]={"$"},texpr[50],expr[50],optbl[50]={"$"};char op[6][6][1]={/*+-id$*//*+*/'>','>','','',/*/*/'>','>','>','','>','>','>','>','>',/*$*/'