ccnew. final anant
TRANSCRIPT
-
8/8/2019 Ccnew. Final Anant
1/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 1
Aim: Write a program to implement the lexical analyzer.Software Required:Turbo C Compiler
Knowledge Required: Concepts of lexical analysis.Theory/Logic:
Lexical analysis is a function in which the stream of characters making upthe source program is read from left to right and grouped into tokens that aresequences of characters having a collective meaning.
In a compiler, lexical analysis is called linear analysis or Scanning.
A lexical analyzer reads and converts the input into a stream of tokens to beanalyzed by the parser.
The sentences of a language consist of string of tokens.
A sequence of input characters that comprises a single token is called alexeme.
A lexical Analyzer can insulate a parser from the lexeme representation oftokens.
A lexical analyzer generates tokens for any program that is given as an inputto it.
It mainly generates 7 types of tokens: keywords, identifiers, constants,literals, special symbols, operators, comments.
Hasmukh Goswami College Of Engg.Vahelal
SOURCECODE
LEXICALANALYZER
TOKENS
-
8/8/2019 Ccnew. Final Anant
2/56
Shah Anant S 200705A01099Batch:C1 2010-11
Source Code:
#include#include#define MAX 30
void main(){
char str[MAX];int state=0;int i=0, j, startid=0, endid, startcon, endcon;FILE *fp;clrscr();fp=fopen("lex.txt","r");printf("Given Grammar:\n");while(!feof(fp)){
str[i]=getc(fp);printf("%c",str[i]);i++ ;switch(state){
case 0: if(str[i]=='i') state=1; //ifelse if(str[i]=='w') state=3; //whileelse if(str[i]=='d') state=8; //doelse if(str[i]=='e') state=10; //elseelse if(str[i]=='f') state=14; //forelse if(isalpha(str[i]) || str[i]=='_')
{state=17;startid=i;
} //identifiers
else if(str[i]=='='else if(str[i]=='=') state=23; //relational '==' or '='else if(isdigit(str[i])){
state=25; startcon=i;
} //constant.
else if(str[i]=='(') state=26; //special characters '('else if(str[i]==')') state=27;//special characters ')'else if(str[i]==';') state=28; //special characters ';'else if(str[i]=='+') state=29; //operator '+'else if(str[i]=='-') state=30; //operator '-'
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
3/56
Shah Anant S 200705A01099Batch:C1 2010-11
break;//States for 'if'case 1: if(str[i]=='f') state=2;
else { state=17; startid=i-1; i--; }
break;
case 2: if(str[i]=='(' || str[i]==NULL){
printf("\nif : Keyword\n");state=0;i--;
}else { state=17; startid=i-2; i--; }
break;
//States for 'while'case 3: if(str[i]=='h') state=4;
else { state=17; startid=i-1; i--; }break;
case 4: if(str[i]=='i') state=5;else { state=17; startid=i-2; i--; }break;
case 5: if(str[i]=='l') state=6;else { state=17; startid=i-3; i--; }break;
case 6: if(str[i]=='e') state=7;
else { state=17; startid=i-4; i--; }break;
case 7: if(str[i]=='(' || str[i]==NULL){
printf("\nwhile : Keyword\n");state=0;i--;
}else { state=17; startid=i-5; i--; }break;
//States for 'do'case 8: if(str[i]=='o') state=9;
else { state=17; startid=i-1; i--; }break;
case 9: if(str[i]=='{' || str[i]==' ' || str[i]==NULL ||str[i]=='('){
printf("\ndo : Keyword\n");
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
4/56
Shah Anant S 200705A01099Batch:C1 2010-11
state=0;i--;
}break;
//States for 'else'case 10: if(str[i]=='l') state=11;
else { state=17; startid=i-1; i--; }break;
case 11: if(str[i]=='s') state=12;else { state=17; startid=i-2; i--; }break;
case 12: if(str[i]=='e') state=13;else { state=17; startid=i-3; i--; }break;
case 13: if(str[i]=='{' || str[i]==NULL){
printf("\nelse : Keyword\n");state=0;i--;
}else { state=17; startid=i-4; i--; }
break;
//States for 'for'case 14: if(str[i]=='o') state=15;
else { state=17; startid=i-1; i--; }break;
case 15: if(str[i]=='r') state=16;else { state=17; startid=i-2; i--; }break;
case 16: if(str[i]=='(' || str[i]==NULL){
printf("\nfor : Keyword\n");state=0;i--;
}
else { state=17; startid=i-3; i--; }break;
//States for identifierscase 17:if(isalnum(str[i]) || str[i]=='_'){
state=18; i++;}
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
5/56
Shah Anant S 200705A01099Batch:C1 2010-11
elseif(str[i]==NULL||str[i]==''||str[i]=='('||str[i]==')'||str[i]==';'||str[i]=='='||str[i]=='+'||str[i]=='-') state=18;i--;break;
case 18:if(str[i]==NULL || str[i]=='' || str[i]=='(' ||str[i]==')' || str[i]==';' || str[i]=='=' || str[i]=='+' ||str[i]=='-'){
endid=i-1;printf(" ");for(j=startid; j
-
8/8/2019 Ccnew. Final Anant
6/56
Shah Anant S 200705A01099Batch:C1 2010-11
printf("\n>= : Relational operator\n");i--;state=0;
}
break;
//States for relational operator '==' & assignment operator '='case 23: if(str[i]=='=') state=24;
else{
printf("\n= : Assignment operator\n");i--;state=0;
}break;
case 24: if(isalnum(str[i])){
printf("\n== : Relational operatorn\n");state=0;i--;
}break;
//States for constantscase 25: if(isalpha(str[i]))
{
printf(" *** ERROR ***");puts(str);for(j=0; j
-
8/8/2019 Ccnew. Final Anant
7/56
Shah Anant S 200705A01099Batch:C1 2010-11
}break;case 26: printf("\n( : Special character\n");
//State for special character '('startid=i;state=0;i--;break;case 27: printf("\n) : Special character\n");
//State for special character ')'state=0;i--;break;
case 28: printf("\n; : Special character\n");
//State for special character ';'state=0;i--;break;case 29: printf("\n+ : Operator\n");
//State for operator '+'state=0;i--;
break;case 30: printf("\n+ : Operator\n");
//State for operator '-'state=0;i--;break;
}i++;getch();
}
Output:-The grammer is: for(x1=0; x1
-
8/8/2019 Ccnew. Final Anant
8/56
Shah Anant S 200705A01099Batch:C1 2010-11
x1 : Identifier
-
8/8/2019 Ccnew. Final Anant
9/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 2
Aim: Write a program to left factoring of the given grammar.S=iEiSeS|iEts|b
Software Required:Turbo C CompilerKnowledge Required: Algorithm to remove the Left Factoring from the given grammar.Theory/Logic:
Left factoring is a grammar transformation that is useful forproducing a grammar suitable for predictive parsing.
The basic idea is that when it is not clear which of the twoalternative productions to use to expand a non terminal A ,we may be able torewrite the A production to defer the decision until we have seen enough ofthe input to make the right choice.
Algorithm:
Input: Grammar G with no cycles or productions.
Output: An equivalent grammar with no left recursion.
Method:
for each non terminal A find the longest prefix a common to two ormore of its alternatives.
If a!=null replace all the A productions A->aB |aB |.Y where Yrepresents all the alternatives that do not begin with a byA->aA|YA->B|B|..B
Here A is the new non terminal repeatedly apply this alternative untilno two alternative have common prefix
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
10/56
Shah Anant S 200705A01099Batch:C1 2010-11
Source Code:
#include#include#include
FILE *fsrc;void main(){
int i=0,j=0,k=0,l=0,m=0,temp=0,a=0,len1,len2;char grammar[100], nt[5][10][10], dummy[2]="\0", final[10]="\0",final1[10]="\0";clrscr();fsrc=fopen("grammer.txt","r");printf("Given Grammar:\n");while(!feof(fsrc)){
grammar[i]=getc(fsrc);printf("%c",grammar[i]);i++;
}for(j=0;j
-
8/8/2019 Ccnew. Final Anant
11/56
Shah Anant S 200705A01099Batch:C1 2010-11
{
nt[k][l][m]='\0'; l++;temp=0;m=0;
}}else{
nt[k][l][m]='\0';k++;l=0;m=0;
}}len1=strlen(nt[0][0]);
len2=strlen(nt[0][1]);for(i=0;i
-
8/8/2019 Ccnew. Final Anant
12/56
Shah Anant S 200705A01099Batch:C1 2010-11
}printf("\n\nAfter Left Factoring the Grammer is:");printf("\n%c->%sS'|%s\nS'->%s|@",grammar[0],final1,nt[0][2],final);printf("\n%c->%c",dummy[0],nt[1][0][0]);
getch();}
Output:
Given grammer S=iEtSeS|iEtS|bS1 : iEtSeSS2 : iEtSS3 : bS=iEtSA|bA=eS|#
Advantage:
Left factoring is d grammar transformation that is useful for producing a grammarsuitable for predictive parsing.
We can eliminate repetitive grammar productions.
Parsing would become more efficient and we can distingue between two alternativesin order to predict optimum alternative for the given non terminal
Disadvantage:
There might be in the grammar that no left factoring is there so in that case this
method is not useful in parsing. The program requires more data structure in order to make left factoring possible
because the new non terminals have to be added, and if the grammar contains morealternatives than there would be memory over flow problem.
Application: Syntax Analyzer in compilers.Conclusion:
Left factoring requires to find out the longest prefix in each right side productionalternatives in order to make top down parsing without backtracking successful.Question: Give another example of the left factoring to properly understand the method?The grammar is:
S->iEts | itSes | aE->b
Answer: By eliminating left factoring,S->iEtSS | aS->eS | @E-> b
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
13/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 3
Aim: Write a program to remove the Left Recursion from a given grammar.Software Requirements:Turbo C Compiler
Knowledge Requirement: Left Recursion Technique for grammarsTheory/Logic: Top-down parsing methods can not handle left-recursive grammars.So a transformation that eliminates left-recursion is needed.This program will eliminate left-recursion problem of given grammar.Algorithm:
Input: Grammar G with no cycles or null-productions.Output:An equivalent grammar with no left recursion.Method:Apply the algorithm given below to G.Note that the resulting non-left-recursive grammar may have null-productions.1. Arrange the non terminals in some order A1, A2 ,..An.2. for i:=1 to n do begin
for j:=1 to i-1 do beginReplace each production of the form Ai->Ajyby the production Aj->s1y|s2y|.|sky.
Where Aj->s1|s2||sk are all the current Aj-productions;EndEliminate the immediate left recursion among the Ai-productions.End
Source Code:
#include#include
#includevoid ELR(char nonterminal);FILE *fsrc,*fpw;int i,j,k,l,m,i1;char nt[10][10][10],grammar[100];void main(){
clrscr();fsrc=fopen("left.txt","r");printf("Given Grammar\n");i=k=l=m=0;
while(!feof(fsrc)){
grammar[i]=getc(fsrc);printf("%c",grammar[i]);i++;
}printf("\n\nAfter Elimination of Left Recursion (For Above Grammar):");for(j=0;j
-
8/8/2019 Ccnew. Final Anant
14/56
Shah Anant S 200705A01099Batch:C1 2010-11
{
if(grammar[j]!='\n'){
if(grammar[j]!='|')
{ nt[k][l][m]=grammar[j];m++;
}else{
nt[k][l][m]='\0';l++;m=0;
}}else
{nt[k][l][m]='\0';k++;l=0;m=0;
}}for(i1=0;i1
-
8/8/2019 Ccnew. Final Anant
15/56
Shah Anant S 200705A01099Batch:C1 2010-11
T T * F | FF (E) | id
E TEE +TE | T FTT *FT | F (E) | id
Advantage:Top-down parsing methods cannot handle left-recursive grammars, so atransformation that eliminates left recursion is needed.
Disadvantage:It is not easier process for each and every grammar to eliminate left recursion.
There might be in the grammar that no left recursion is there so in that case this method is notuseful in parsing.
Application: Syntax Analyzer in compilers.
Conclusion:Removing Left recursion requires to eliminate the same non terminal in left and
right hand side production alternatives in order to remove the infinite looping of the
productions as parsing process goes on.
Questions:
1. Give another example of the left recursion to properly understand the method?The grammar is:
S->Aa | bA-> Ac | Sd | @
Answer:By eliminating left-recursion,S-> Aa | b
A-> bdA | AA->cA| adA | #
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
16/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 4
Aim: Write a C program for finding a FIRST set of all non-terminals of a given grammar.E TE
E +TE | T FTT *FT | F (E) | id
Software Required:Turbo C CompilerKnowledge Required: Concepts of First set of the grammar.
What is first and what is included in the first set of the non terminal.Theory/Logic:
If is any string of grammar symbols, let FIRST() be the set of terminals that begin
the strings derived from . If =*> , then is also in FIRST(). The construction of a predictive parser is aided by two functions associated with a
grammar G. These functions, FIRST and FOLLOW, allow us to fill in the entries of a
predictive parsing table for G, whenever possible. Sets of tokens yielded by the
FOLLOW function can also be used as synchronizing tokens during panic-mode error
recovery.
To compute FIRST(X) for all grammar symbols X, the following rules are used until
no more terminals or can be added to any FIRST set.
1. If X is terminal, then FIRST(X) is {X}.
2. If X is a production, then add to FIRST(X).
3. If X is a non-terminal and X Y1 Y2 . . . Ykis a production, then place in
FIRST(X) if for some i, is in FIRST(Y1), and is in all of FIRST(Y1), . . .
FIRST(Yi-1); that is Y1 Y2 . . . Yi-1 => . If is in FIRST(Yj) for all j = 1,2, . . .
. ,k, then add to FIRST(X).
For example, everything in FIRST(Y1) is surly in FIRST(X). If Y1
does not derive , then we add nothing more to FIRST(X), but if Y1 => , then we add
FIRST(Y2) and so on.
Source Code:
#include#include
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
17/56
Shah Anant S 200705A01099Batch:C1 2010-11
#includevoid Find_First(char nt);
FILE *fsrc,*fpw;
int i=0,j,k=0,l=0,m=0,i1,i2;char nt[10][10][10],grammar[100];void main(){
clrscr();fsrc=fopen("grammar.txt","r");if(fsrc==NULL){
printf("\nThe File Cant be opened");}else
{printf("Given Grammar\n");while(!feof(fsrc)){
grammar[i]=getc(fsrc);printf("%c",grammar[i]);i++;
}printf("\n\nFIRST SET (For Above Grammar):");for(j=0;j
-
8/8/2019 Ccnew. Final Anant
18/56
Shah Anant S 200705A01099Batch:C1 2010-11
m=0;
}}for(i2=0;i1,i2
-
8/8/2019 Ccnew. Final Anant
19/56
Shah Anant S 200705A01099Batch:C1 2010-11
E +TE | T FTT *FT | F (E) | id
First Set:
FIRST(E)={ ( , id }
FIRST(E)={ + , }
FIRST(T)={ ( , id }
FIRST(T)={ * , }
FIRST(F)={ ( , id }
Advantages: Use to generate parse table.To find FOLLOW of given grammar.
Disadvantage:It is some what tedious to understand the algorithm.
Conclusion:
FIRST is the function, used in the construction of predictive parser.
If is any string of grammar symbols, let FIRST() be the set ofterminals that begin the strings derived from .
Questions:
1) Find the FIRST of given grammar:
S=ABaA=aaBB=bBB=^
Answer:
FIRST(S)=FIRST(A)={a}FIRST(B)={b}{^}FIRST(B)={b}{^}
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
20/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 5
Aim: Write a C program for finding a FOLLOW set of all non-terminals of given grammar.E TE
E +TE | T FTT *FT | F (E) | id
Software Required:Turbo C CompilerKnowledge Required: Concepts of Follow set of the Grammar.
The whole knowledge of the First set of the Grammar.Theory/Logic:
Define FOLLOW(A), for non-terminal A, to be the set of terminals that can
appear immediately to the right of A in some sentential form, that is, the set ofterminals such that there exist a derivation from S=> A| B for some andB.
To compute Follow(A) for all non-terminals A, the following rules are usedu8ntill nothing can be added to any Follow set:
1. Place $ in Follow(S), where S is the starting symbol and $ is the inputright end marker.
2. If there is a production A-> B, then everything in First() except for
NULL is placed in Follow(B).
3. If there is a production A-> B, or a production A-> B where First()contains NULL, then every thing in Follow(A) is in Follow(B)
Source Code:
#include#include#include#includechar non_terminals[10];char statement[10][80],no_of_non_terminal;
char str_temp[40],pos;int first_for_follow(char);int lines,flag;char *t;void sep_pipes(){
FILE *fp1,*fp2;char str[79],statement[10][80];
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
21/56
Shah Anant S 200705A01099Batch:C1 2010-11
int i,j,k,l,lines,pipe_sym;char ch;char temp_str[10];clrscr();
fp1=fopen("source.txt","r");if(!fp1){
printf("Grammar not Found");getch();exit(0);
}printf("\t\t\t This is My Grammar\n");lines=0;while(fgets(str,79,fp1)){
printf("%s",str);strcpy(statement[lines],str);lines++;
}fp2=fopen("temp.txt","w");for(i=0;i
-
8/8/2019 Ccnew. Final Anant
22/56
Shah Anant S 200705A01099Batch:C1 2010-11
{
FILE *fp1,*fp2;char *temp;char str[79];
int i,j,k,l;char temp_str[10];char* find_first(char);char* find_follow(char);//int first_for_follow(char);fp1=fopen("temp.txt","r");lines=0;sep_pipes();getch();while(fgets(str,79,fp1)){
strcpy(statement[lines],str);lines++;
}getch(); printf("\n\t\t\t Finding FIRST\n\n\n");for(i=0;i
-
8/8/2019 Ccnew. Final Anant
23/56
Shah Anant S 200705A01099Batch:C1 2010-11
printf("FIRST( %c )",non_terminals[i]);strcpy(str_temp,"");pos=0;find_first(non_terminals[i]);
printf(" ==> { %s }",str_temp);printf("\n");
} printf("\n\n\t\t\t Finding FOLLOW\n\n\n");for(i=0;i
-
8/8/2019 Ccnew. Final Anant
24/56
Shah Anant S 200705A01099Batch:C1 2010-11
for(i=0;i
-
8/8/2019 Ccnew. Final Anant
25/56
Shah Anant S 200705A01099Batch:C1 2010-11
{first_for_follow(statement[line_no][b_pos+1]);}else
{temp = find_follow(statement[line_no][0]);return str_temp;}
}}
}return str_temp;
}int first_for_follow(char ch){
int i,j;int flag=0;for(i=0;i
-
8/8/2019 Ccnew. Final Anant
26/56
Shah Anant S 200705A01099Batch:C1 2010-11
if(statement[i][2]!='^'){
str_temp[pos]=statement[i][2];pos++;
str_temp[pos]='\0';}
}}
}else{
if(ch!='^'){
str_temp[pos]=ch;pos++;
str_temp[pos]='\0';}
}}
Output: Source.txt
E TE E +TE |
T FT T *FT |
F (E) | id
FOLLOW(E) = { ) , $ }
FOLLOW(E) = { ) , $ }
FOLLOW(T) = { + , ) , $ }
FOLLOW(T) = { + , ) , $ }
FOLLOW(F) = { + , * , ) , $ }
Advantages: Allows us to fill the entries of a predictive parsing table for G, whenever
possible.Application: In the construction of a predictive parser
Sets of tokens yielded by the FOLLOW function can also be used assynchronizing tokens during panic-mode error recovery.
Conclusion:Allows us to fill in the entries of a predictive parsing table for G, wheneverpossible and helps in construction of a predictive parser.
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
27/56
Shah Anant S 200705A01099Batch:C1 2010-11
Question: Define FOLLOW set of Grammar G?Answer:
Define FOLLOW(A), for non-terminal A, to be the set of terminals that canappear immediately to the right of A in some sentential form, that is, the set of
terminals such that there exist a derivation from S=> A| B for some and B.
Question: How is Panic Mode error recovery performed?Answer:
Panic mode error recovery is based on the simple idea of skipping symbols on theinput until a token is a selected set of synchronizing tokens appears.
As a stating point during Panic mode recovery, we can place all symbols inFollow(A) into the synchronization set of non-terminal A.
If we skip the tokens until an element of Follow(A) is seen and pop A from thestack, it is unlikely that the parsing will continue .
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
28/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 6
Aim: Write a C program to parse a given string using Predictive parsing for given grammar.type simple | id | array [ simple ] of type
simple integer | char | num dotdot numSoftware Required:Turbo C Compiler
Knowledge Required: Concepts of Predictive parsingTheory/Logic:
Recursive-decent parsing is a top-down method of syntax analysis in which weexecute set of recursive procedures to process the input. A procedure is associatedwith each non-terminal of a grammar.
Here, we consider a special form of recursive-decent parsing, called predictiveparsing, in which the look-ahead symbol unambiguously determines the procedureselected for each non-terminal.
The sequence of procedures called in processing the input implicitly defines a parsetree for the input.
A predictive parser is a program consisting of a procedure for every non-terminal.Each procedure does two things.1. It decides which production t use by looking at the look-ahead symbol. The
production with right side is used if the look-ahead symbol is in FIRST(). Ifthere is a conflict between two right sides for any look-ahead symbol, then wecannot use this parsing method on this grammar. A production with o the rightside is used if the look-ahead symbol is not in the FIRST set for any other righthand side.
2. The procedure uses a production by mimicking the right side. A non-terminalresults in a call to the procedure for the non-terminal, and a token matching thelook-ahead symbol results in the next input token being read. If at some point the
token in the production does not match the look-ahead symbol, an error isdeclared.
Source code:
#include#include#include#include
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
29/56
Shah Anant S 200705A01099Batch:C1 2010-11
void simple(void);void type(void);void match(char *a);FILE *fsrc,*fpw;
int i,j,k,l;char token[20][20],source[100];char *lookahead,*token1;void main(){
clrscr();src=fopen("one.txt","r");rintf("Given String:\n");i=k=l=0;while(!feof(fsrc)){
source[i]=getc(fsrc);printf("%c",source[i]);i++;
}for(j=0;j
-
8/8/2019 Ccnew. Final Anant
30/56
Shah Anant S 200705A01099Batch:C1 2010-11
}else{
printf("\nERROR: String is not in given Grammar\n");
getch();exit(0);
}}void type(){
if(strcmp(lookahead,"integer")==0 || strcmp(lookahead,"char")==0 ||strcmp(lookahead,"num")==0){
simple();}
else if(strcmp(lookahead,"^")==0){
printf("^ ");match("^");printf("id ");match("id");
}else if(strcmp(lookahead,"array")==0){
printf("array ");match("array");
printf("[ ");match("[");simple();printf("] ");match("]");printf("of ");match("of");type();
}else{
printf("\nERROR: String is not in given Grammar\n");getch();exit(0);
}}void simple(){
if(strcmp(lookahead,"integer")==0)
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
31/56
Shah Anant S 200705A01099Batch:C1 2010-11
{
printf("integer ");match("integer");
}
else if(strcmp(lookahead,"char")==0){
printf("char ");match("char");
}else if(strcmp(lookahead,"num")==0){
printf("num ");match("num");printf("dotdot ");match("dotdot");
printf("num ");match("num");
}else{
printf("\nERROR: String is not in given Grammar\n");getch();exit(0);
}}
Input: one.txtarray [ num dotdot num ] of integer
Output:Given String:array [ num dotdot num ] of integerString Parse Up To:array [ num dotdot num ] of integerSuccessfully Parse
Application:-For parsing a string.
Advantage:-To construct a string translator predictive parser is used.Syntax directed translator can be form extending a predictive grammer.
Questions:-
1). what is predictive parser?
Ans:- It is a program consisting of a procedure for every non terminal.
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
32/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 7
Aim: Write a C program for finding some kind of error in program.
Software Required:Turbo C CompilerKnowledge Required: Concepts of Syntax analysis.Theory/Logic:
The error detection and reporting is done by each phase of compiler. However, afterdetecting an error, a phase must somehow deal with that error, so that compilation canproceed, allowing further errors in the source program to be detected.
The syntax and semantic analysis phases usually handle large fraction of the errorsdetectable by the complier. The lexical phase can detect errors where charactersremaining in the input do not form any token of the language.
We know that program can contain errors at many different levels. For e.g., errors canbe
o Lexical, such as misspelling and identifier, keyword, or operator.o Syntactic, such as an arithmetic expression with unbalanced parentheses.
o Semantic, such as an operator applied to an incompatible operand.
o Logical, such as an infinitely recursive call.
In this program I have tried to detect some kind of errors such asSemicolon (;) missing in expression.
Unbalance pair of parenthesis.
The structure of if loop doesnt match.
The structure of for loop doesnt match.
Source code:-
#include#include#includevoid main(int argc,char *argv[]){
char str[80],prev,ch;int flag=0,fflag=0,tflag=0,iflag=0,bflag=0,eflag=0,line=0;FILE *p;clrscr();p=fopen(argv[1],"r");
if(p==NULL){
printf("can not open the file");exit();
}while(fscanf(p,"%s",str)!=EOF){
if(strcmp(str,"tushar()")==0)
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
33/56
Shah Anant S 200705A01099Batch:C1 2010-11
tflag=1;
else if(strcmp(str,"{")==0){
if(tflag==1)
bflag=1;else{
flag=1;printf("\nerror on line 1");break;
}}else if(strcmp(str,"Tint")==0){
if(bflag==1)
iflag=1;else{
flag=1;printf("\nerror on line 2");break;
}}else if(strcmp(str,"Tfloat")==0){
if(iflag==1)
fflag=1;else{
flag=1;printf("\nerror on line 3");break;
}}else if(strcmp(str,"}")==0){
if(fflag==1)
eflag=1;else{
flag=1;printf("\nerror on line 4");break;
}}
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
34/56
Shah Anant S 200705A01099Batch:C1 2010-11
}fclose(p);p=fopen("text.txt","r");if(p==NULL)
{printf("can not open the file");exit();
}while((ch=getc(p))!=EOF){
if(ch=='\n'){
line++;if(prev==')'| prev=='{'| prev=='}')
continue;
if(prev!=';'){
flag=1;printf("\nerror on line %d ,semicolon missing",line);
}}prev=ch;
}fclose(p);if(flag==1)
printf("\n incorrect");
elseprintf("\n correct");
getch();}
Output:-
Input string: text.txt
tushar(){
Tint aTfloat b=1.3;
a=b;}
Output string:-
Error on line 3, semicolon missingIncorrect
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
35/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 8
Aim: Write a C program for finding some kind of error in program
Source code:-
#include#include#includevoid main(int argc,char *argv[]){
char str[80],prev,ch;
int flag=0,fflag=0,tflag=0,iflag=0,bflag=0,eflag=0,line=0;FILE *p;clrscr();p=fopen(argv[1],"r");if(p==NULL){
printf("can not open the file");exit();
}while(fscanf(p,"%s",str)!=EOF){
if(strcmp(str,"tushar()")==0)tflag=1;else if(strcmp(str,"{")==0){
if(tflag==1)bflag=1;
else{
flag=1;printf("\nerror on line 1");break;
}}else if(strcmp(str,"Tint")==0){
if(bflag==1)iflag=1;
else{
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
36/56
Shah Anant S 200705A01099Batch:C1 2010-11
flag=1;printf("\nerror on line 2");break;
}
}else if(strcmp(str,"Tfloat")==0){
if(iflag==1)fflag=1;
else{
flag=1;printf("\nerror on line 3");break;
}
}else if(strcmp(str,"}")==0){
if(fflag==1)eflag=1;
else{
flag=1;printf("\nerror on line 4");break;
}
}}fclose(p);p=fopen("text.txt","r");if(p==NULL){
printf("can not open the file");exit();
}while((ch=getc(p))!=EOF){
if(ch=='\n'){
line++;if(prev==')'| prev=='{'| prev=='}')
continue;if(prev!=';'){
flag=1;
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
37/56
Shah Anant S 200705A01099Batch:C1 2010-11
printf("\nerror on line %d ,semicolon missing",line);
}}prev=ch;
}fclose(p);if(flag==1)
printf("\n incorrect");else
printf("\n correct");getch();
}
Output:-
Input string: text.txt
func(){
Tint aTfloat b=1.3;a=b;
}
Output string:-
Error on line 3, semicolon missingIncorrect
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
38/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical: 9
Aim: Write a program for simple calculator having support of +, - , * , / and unary minus
operators with LEX and YACC.
Source code ( Simple Calculator ):
FILE : hoc.y
%
{
#define YYSTYPE double /* data type of yacc stack */
%
}
%token NUMBER
%left + - /* left associative, same precedence */
%left * / /* left associative, higher precedence */
%%
list: /* nothing */
| list \n
| list expr \n { printf(\t%.8g\n,$2); }
expr: NUMBER { $$ = $1; }| expr + expr { $$ = $1 + $3; }
| expr - expr { $$ = $1 - $3; }
| expr * expr { $$ = $1 * $3; }
| expr / expr { $$ = $1 / $3; }
| ( expr ) { $$ = $2; }
;
%%
/* end of grammar */
#include
#include
char *progname; /* for error message */
int lineno = 1;
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
39/56
Shah Anant S 200705A01099Batch:C1 2010-11
main(argc, argv) /* hoc1 */
char *agrv[];
{
progname = argv[0];
yyparse();
}
yylex() /* Continuing in hoc.y */
{
int c;
while ( ( c=getchar() ) = = || c = = \t );
if ( c = = EOF )
return 0;
if ( c = = . || isdigit ( c ) ) /* number */
{
ungetc(c, stdin);
scanf(%lf, &yylval);
return NUMBER;
}
if ( c = = \n )
lineno++;
return c;
}
yyerror(s) /* called for yacc syntax error */
char *s;
{
warning( s, ( char * ) 0);
}
warning(s, t) /* print warning message */
char *s, *t;
{
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
40/56
Shah Anant S 200705A01099Batch:C1 2010-11
fprintf(stderr, %s: %s , progname, s);
if ( t )
fprintf(stderr, %s, t);
fprintf(stderr, near line %d \n, lineno);
}
Output:
Compilation of a yacc program is a two-strp process:
$ yacc hoc.y
$ cc y.tab.c o hoc1
$ hoc1
2/3
0.66666667
-3-4
hoc1: syntax error near line 1
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
41/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical: 10
Aim: Write a program for calculator with arbitrary variable names and built-in functions.
This program adds several major new capabilities to program in practical-8 and
practical-9. The main new feature is access to built-in functions:
sin cos atan exp log log10 sqrt int abs
We have also added an exponentiation operator ^, it has highest precedence, and is
right associative.
The program that results from all these changes is big enough that it is best split into
separate files for easier editing and faster compilation. There are now five files
instead of one:
hoc.y Grammar, main, yylex
hoc.h Global data structure for inclusion
symbol.c Symbol table routines, lookup, install
init.c Built-in and constants, init
math.c Interfaces to math routines, sqrt, log, etc.
Since the program is now lives on five files, not one, the makefile is more
complicated:
$ cat makefile
YFLAGS = -d # force creation of y.tab.h
OBJS = hoc.o init.o math.o symbol.o # abbreviation
hoc3: $ ( OBJS )
cc $ ( OBJS ) lm -o hoc3
hoc.o: hoc.h
init.o symbol.o: hoc.h y.tab.h
pr:
@pr hoc.y hoc.h init.c math.c symbol.c makefile
clean:
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
42/56
Shah Anant S 200705A01099Batch:C1 2010-11
rm f $ ( OBJS ) y.tab. [ ch ]
$
$ make -n
$ make pr | lpr
$ make clean
Source code( Calculator with built-in functions ):
FILE : hoc.h
typedef struct Symbol
{ /* symbol table entry */
char *name;
short type; /* VAR, BLTIN, UNDEF */
union
{
double val; /* if VAR */
double ( *ptr ) ( ); /* if BLTIN*/
} u;
struct Symbol *next; /* to link to another */
} Symbol;
Symbol *install( ), *lookup( ) ;
FILE : symbol.c
#include hoc.h
#include y.tab.h
static Symbol *symlist = 0; /* symbol table: linked list */
Symbol *lookup( s ) /* find s in symbol table */
Char *s;
{
Symbol *sp;
for ( sp = symlist; sp ! = (Symbol * ) 0; sp = sp -> next )
if ( strcmp( sp -> name, s) = = 0 )
return sp;
return 0;
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
43/56
Shah Anant S 200705A01099Batch:C1 2010-11
}
Symbol *install( s, t, d) /* install s in symbol list */
char *s;
int t;
double d;
{
Symbol *sp;
char *emalloc( ) ;
sp = ( Symbol * ) emalloc ( sizeof ( Symbol ) );
sp -> name = emalloc ( strlen ( s ) + 1 ) ; /* + 1 for \0 */
strcpy(sp -> name, s);
sp -> type = t; sp -> u.val = d;
sp -> next = symlist; /* put at front of list */
symlist = sp;
return sp;
}
char * emalloc ( n) /* check return from malloc */
unsigned n;
{
char *p, *malloc ( );
p = malloc ( n );
if ( p = = 0 )
execerror ( out of memory, ( char * ) 0 );
}
FILE : init.c
#include hoc.h
#include y.tab.c
#include
extern double Log( ), Log10( ), Exp( ), Sqrt( ), integer( );
static struct
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
44/56
Shah Anant S 200705A01099Batch:C1 2010-11
{
char *name;
double cval;
}
consts[] = { PI, 3.14159265358979323846,
E, 2.71828182845904523536,
GAMMA, 0.57721566490153286060, /* Euler */
DEG, 57.29577951308232087680, /* deg/radian */
PHI, 1.61803398874989484820, /* golden ratio */
0, 0};
static struct
{
char *name;
double (*func) ( );
} /* Built-ins */
built-ins[] = { sin, sin,
cos, cos,
atan, atan,
log, Log, /* checks argument */
log 10, Log 10, /* checks argument */
exp, Exp, /* checks argument */
sqrt, Sqrt, /* checks argument */
int, integer,
abs, fabs,
0, 0};
init( ) /* install constants and built-ins in table */
{
int i;
Symbol *s;
for (i = 0; consts[i].name; i++)
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
45/56
Shah Anant S 200705A01099Batch:C1 2010-11
install (consts[i].name, VAR, consts[i].cval);
for (i = 0; builins[i].name; i++)
{
s = install( built-ins[i].name, BLTIN, 0.0 );
s -> u.ptr = built-ins[i].func;
}}
FILE : hoc.y
%
{
#include hoc.h
extern double Pow( );
%
}
%union
{
double val; /* actual value*/
Symbol *sym;
} /* symbol table pointer */
%token NUMBER
%token VAR BLTIN UNDEF
%type expr asgn
%right =
%left + - /* left associative, same precedence */
%left * / /* left associative, higher precedence */
%left UNARYMINUS
%right ^ /* exponentiation */
%%
list: /* nothing */
| list \n
| list asgn \n
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
46/56
Shah Anant S 200705A01099Batch:C1 2010-11
| list expr \n { printf(\t%.8g\n,$2); }
| list error \n { yyerrok; }
asgn: VAR = { $$ = $1 -> u.val = $3; $1 -> type = VAR; }
;
expr: NUMBER
| VAR { if (S1 -> type = = UNDEF)
execerror(undefined variable:, $1 -> name);
$$ = $1 -> u.val; }
| asgn
| BLTIN ( expr ) { $$ = ( * ( $1 -> u.ptr ) ) ( $3 ); }
| expr + expr { $$ = $1 + $3; }
| expr - expr { $$ = $1 - $3; }
| expr * expr { $$ = $1 * $3; }
expr / expr {
if ( $3 = = 0.0 )
execerror (division by zero , );
$$ = $1 / $3; }
expr ^ expr { $$ = Pow( $1, $3); }
| ( expr ) { $$ = $2; }
| - expr %prec UNARYMINUS { $$ = -$2; }
%% /* end of grammar */
#include
#include
jmp_buf begin;
main(argc, argv) /* hoc3 */
char *agrv[];
{
int fpecatch();
progname = argv[0];
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
47/56
Shah Anant S 200705A01099Batch:C1 2010-11
init( );
setjmp(begin);
signal(SIGFPE, fpecatch);
yyparse();
}
execerror(s, t) /* recover from run-time error */
char *s, *t;
{
warning(s, t);
longjmp(begin, 0);
}
fpecatch() /* catch floating point exceptions*/
{
execerror(floating point exception, (char *) 0 ); }
yylex() /* Continuing in hoc.y */
{
int c;
while ( ( c=getchar() ) = = || c = = \t );
if ( c = = EOF )
return 0;
if ( c = = . || isdigit ( c ) ) /* number */
{
ungetc(c, stdin);
scanf(%lf, &yylval.val);
return NUMBER;
}
if(islower(c) )
{
yylavl.index = c a; /* ASCII only */
}
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
48/56
Shah Anant S 200705A01099Batch:C1 2010-11
if(isalpha(c) )
{
Symbol *s;
char sbuf(100), *p = sbuf;
do
{
*p++ = c;
}
while ( ( c = getchar( ) ) != EOF && isalnum(c) );
ungetc(c, stdin);
*p = \0;
if( ( s = lookup(sbuf) ) = = 0 )
s = install(sbuf, UNDEF, 0.0 );
yylval.sym = s;
return s-> type = = UNDEF ? VAR : s -> type;
}
if ( c = = \n )
lineno++;
return c;
}
yyerror(s) /* called for yacc syntax error */
char *s;
{
warning( s, ( char * ) 0);
}
warning(s, t) /* print warning message */
cahr *s, *t;
{
fprintf(stderr, %s: %s , progname, s);
if ( t )
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
49/56
Shah Anant S 200705A01099Batch:C1 2010-11
fprintf(stderr, %s, t);
fprintf(stderr, near line %d \n, lineno);
}
FILE: math.c
#include
#include
extern int errorno;
double errcheck( );
double Log( x )
double x;
{
return errcheck( log( x ), log );
}
double Log10( x )
double x;
{
return errcheck( log10( x ), log10 );
}
double Exp( x )
double x;
{
return errcheck( exp( x ), exp );
}
double Sqrt( x )
double x;
{
return errcheck( sqrt( x ), sqrt );
}
double Pow( x , y )
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
50/56
Shah Anant S 200705A01099Batch:C1 2010-11
double x , y;
{
return errcheck( pow( x , y ), exponentiation );
}
double integer( x )
double x;
{
return ( double ) ( long ) x;
}
double errcheck( d, s ) /* check result of library call */
double d;
char *s;
{
if ( erno = =EDOM )
{
errno = 0;
execerror( s, argument out of domain);
}
else if ( errno = = ERANGE )
{
errno = 0;
execerror( s, result out of range );
}
return d;
}
Output:
$ hoc3
1.5 ^ 2.3
2.5410306
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
51/56
Shah Anant S 200705A01099Batch:C1 2010-11
exp(2.3 * log( 1:5 ) )
2.5410306
sin(PI / 2)
1
atan ( 1 )*DEG
45
x = 2 * 3.14159
6.28318 /* value printed for assignment to variable */
x = 1 /* Assignment: no value is printed */
x /* Expression*/
6.28318 /* value is printed */
Application:-Used for Generating C code by just inputting the grammar to YACC.
Advantages:- For easily constructing the parser from input a grammar.
Syntax Directed translator can be formed extending a predictive parser.
Questions:-
1).What is YACC?
- YACC is inbuilt utility of Linux operating system. This utility is used to generating the
C code for parsing from a given grammar as an input.
2).What is LEX?
- YACC is inbuilt utility of Linux operating system. This utility is used to generating the C
code of Lexical analyzer from given set of rules as an input.
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
52/56
Shah Anant S 200705A01099Batch:C1 2010-11
Practical - 11
Aim: Write a program to perform Recursive Descendent Parsing for Grammar given below.E -> T + E / TT -> F * T / FF -> ( E ) / i
Software Required: Turbo C or Borland CKnowledge Required: Top-Down Parsing, Recursive Descent Parsing.Theory/Logic:This is a Top-down Parsing Method without backtracking. It is used to checkthe syntax of statements. If any syntax errors are present in the statement then error messageswill be generated as output.
This Program required four functions.
1. Get_Char()
This function returns next character from the input string and assign it to the global variablenext. It also increment the cursor pointer.
2. Expr()
This is recursive function.It checks for E -> T + E / T grammar.
3. Term()
This is recursive function.It checks for T -> F * T / F grammar.
4. Factor()
It is also recursive function.It checks for F -> ( E ) / i grammar.
Source Code:-
#include
#include# define TRUE 1# define FALSE 0char next;char *input;int cursor;char Get_Char();int Expr();
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
53/56
Shah Anant S 200705A01099Batch:C1 2010-11
int Term();int Factor();void main(){
cursor=0;clrscr();printf("The Grammar is::\n\n");printf("E -> T+E / T\nT -> F*T / F\nF -> (E) / i\n\n");printf("Enter String For RDP(Please append '#' at end od input):: ");gets(input);next=Get_Char();if(Expr()){
if(next=='#')printf("\nValid Statement");
elseprintf("\nInvalid Statement");
}else
printf("\nInvalid Statement");getch();
}int Expr(){
if(!Term())return FALSE;
if(next=='+'){
next=Get_Char();if(next=='#')
return FALSE;if(!Expr())
return FALSE;else
return TRUE;}
else
return TRUE;}int Term(){
if(!Factor())return FALSE;if(next=='*'){
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
54/56
Shah Anant S 200705A01099Batch:C1 2010-11
next=Get_Char();if(next=='#')
return FALSE;if(!Term())
return FALSE;else
return TRUE;}else
return TRUE;}int Factor(){
if(next=='#')return FALSE;
if(next=='('){
next=Get_Char();if(next=='#')
return FALSE;if(!Expr())
return FALSE;if(next!=')')
return FALSE;else{
next=Get_Char();return TRUE;
}}if(next!='i')
return FALSE;else{
next=Get_Char();return TRUE;
}
}
char Get_Char(){
char ch;ch=input[cursor];cursor++;return ch;
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
55/56
Shah Anant S 200705A01099Batch:C1 2010-11
}
OUTPUT:: 1:
The Grammar is::
E -> T+E / TT -> F*T / FF -> (E) / i
Enter String For RDP(Please append '#' at end of input): i*(i+i)#
Valid Statement
OUTPUT 2:
The Grammar is:
E -> T+E / TT -> F*T / FF -> (E) / i
Enter String For RDP(Please append '#' at end of input): i+i*
Invalid Statement
Advantage: Early detection of syntax errors saves the compilation time. These are will notpropagate in the next phases.
Disadvantage: It recursively calls functions and thus requires implicit use of stack datastructure. There may be a chance of stack overflow.
Application: Syntax Analyzer in compilers.
Conclusion: RecursiveDescent parser requires one function for each Non-Terminal given in
the grammar.
Questions:
1). what are the other Top-down parsing Techniques?
Ans:-
- Recursive - Descent parsing.- Predictive Parsers.
Hasmukh Goswami College Of Engg.Vahelal
-
8/8/2019 Ccnew. Final Anant
56/56
Shah Anant S 200705A01099Batch:C1 2010-11
- Nonrecursive Predictive Parsing.