· 6 years ago · Apr 09, 2019, 12:12 AM
1//Write a C program to take another C program as input and store it into another file
2#include <stdio.h>
3#include <stdlib.h>
4
5int main()
6{
7 char s[20];
8 int c, k=1;
9 FILE *fp;
10 printf("Enter file name (with extension): ");
11 scanf("%s",s);
12 do{
13 if((fp = fopen(s,"r"))==NULL)
14 {
15 c = getc(stdin);
16 fp = fopen(s,"w");
17 while((c=getc(stdin))!=EOF)
18 putc(c,fp);
19 k=0;
20 }
21 else
22 {
23 printf("Filename exists. Enter new name: ");
24 scanf("%s",s);
25 }
26 }while(k);
27 fclose(fp);
28}
29
30//Write a C program to count the number of lines in the stored C program
31#include <stdio.h>
32#include <stdlib.h>
33
34int main()
35{
36 char s[20];
37 int c, k=1, count=0;
38 FILE *fp;
39 printf("Enter file name (with extension): ");
40 scanf("%s",s);
41 do{
42 if((fp = fopen(s,"r"))!=NULL)
43 {
44 while((c=getc(fp))!=EOF)
45 if(c=='\n')
46 count++;
47 k=0;
48 }
49 else
50 {
51 printf("File does not exist. Enter correct name: ");
52 scanf("%s",s);
53 }
54 }while(k);
55 fclose(fp);
56 printf("No. of lines = %d",count);
57}
58//Write a C program to count the number of different operators in previously stored file
59
60#include <bits/stdc++.h>
61
62using namespace std;
63
64int main(){
65 ifstream infile;
66 infile.open("in.cpp");
67 ofstream outfile;
68 outfile.open("out.cpp");
69
70 int numOps=0;
71 if(infile.is_open()){
72 while(!infile.eof()){
73 string line;
74 infile >> line;
75
76 for(int i=0;i<line.length();i++){
77 if(line[i]=='+' || line[i]=='-' || line[i]=='*' ||
78 line[i]=='/' || line[i]=='%')
79 numOps++;
80 }
81 }
82 }
83 infile.close();
84 outfile.close();
85
86 cout << "No. of operators(+,-,*,/,%) in input file= " << numOps << endl;
87
88 return 0;
89}
90
91//Write a C program to remove all white spaces, tabs and newline commands from stored C program and copy it into another file.
92#include <stdio.h>
93#include <stdlib.h>
94#include <string.h>
95
96int main()
97{
98 char s[20];
99 char t[]="new_";
100 int c, k=1;
101 FILE *ofp, *fp;
102 printf("Enter file name (with extension): ");
103 scanf("%s",s);
104 strcat(t,s);
105 do{
106 if((ofp = fopen(s,"r"))!=NULL)
107 {
108 c = getc(ofp);
109 fp = fopen(t,"w");
110 while((c=getc(ofp))!=EOF)
111 {
112 if(c=='\n'||c=='\t'||c==' ')
113 continue;
114 putc(c,fp);
115 }
116 k=0;
117 }
118 else
119 {
120 printf("File does not exist. Enter correct name: ");
121 scanf("%s",s);
122 strcat(t,s);
123 }
124 }while(k);
125 fclose(fp);
126
127}
128
129//Write a C program to check the single and multiple comment lines in previously stored file.
130#include<bits/stdc++.h>
131using namespace std;
132
133int checkOp ( string s ){
134 int l = s.length();
135 int i =0;
136 while(i!=l-1 && l>=2){
137 if((s[i]=='/'&&s[i+1]=='/')||(s[i]=='/'&&s[i+1]=='*'))
138 return true;
139 i++;
140 }
141 return false;
142
143}
144
145int main(){
146 string line;
147 ifstream in("remove_spaces.cpp");
148 int ctr = 0;
149 while( getline( in, line ) ) {
150 if(checkOp(line)){
151 ctr++;
152 }
153 }
154 cout << "No of comment(s) is : " << ctr << endl;
155 return 0;
156}
157
158//Write a program to check whether a given Identifier is valid or not. Create a symbol table for the same.
159#include <stdio.h>
160#include <string.h>
161
162int compare(char *id, char *kw){
163 int len1, len2,i;
164 len1 = strlen(id);
165 len2 = strlen(kw) - 1;
166
167 if(len1!=len2)
168 return 0; // Different
169 for( i=0; i<len1; i++){
170 if(id[i]!=kw[i])
171 return 0; // Different
172 }
173 return 1; // Same
174}
175
176int valid(char *id){
177 printf("Identifier: %s, Length: %d\n", id, strlen(id));
178 int Valid = 1,i;
179 if(id[0]=='_' || (id[0]>=65 && id[0]<=90) || (id[0]>=97 && id[0]<=122)){
180 for( i=1; i<strlen(id); i++){
181 if(id[i]=='_' || (id[i]>=65 && id[i]<=90) || (id[i]>=97 && id[i]<=122) || (id[i]>='0' && id[i]<='9'))
182 continue;
183 else{
184 Valid = 0;
185 break;
186 }
187 }
188 }
189 else Valid = 0;
190 return Valid;
191}
192
193int match_word(char *id, char *file){
194 FILE *fptr;
195 int MAX = 100, idx = 1, match = -1;
196
197 fptr = fopen(file, "r");
198 char line[MAX];
199 while( fgets(line,MAX,fptr) ){
200 if(compare(id, line) == 1){
201 match = idx;
202 break;
203 }
204 idx++;
205 }
206 return match;
207}
208
209int main(){
210
211 char id[32];
212 printf("Enter an identifier: ");
213 scanf("%s", id);
214 printf("-----------------------------\n");
215
216 // Check whether identifier is valid or not
217 if(valid(id)){
218
219 // Check if it matches with any of the keywords
220 int matched = match_word(id, "Keywords.txt");
221 if(matched!=-1){
222 printf("INVALID: Matched with a keyword\n");
223 }
224 else{
225 printf("Valid Identifier\n");
226 // Append to Symbol Table if not present
227 int present = match_word(id, "SymbolTableIdentifier.txt");
228 if(present!=-1)
229 printf("-> Present at index %d\n", present);
230 else{
231 FILE *fpa = fopen("SymbolTableIdentifier.txt","a");
232 fprintf(fpa, "%s\n", id);
233 fclose(fpa);
234 }
235 }
236 }
237 else
238 printf("ERROR: Invalid Identifier\n");
239 return 0;
240}
241
242//Write a program to check whether a given Keyword is valid or not. Create a symbol table for the same.
243#include <stdio.h>
244#include <string.h>
245
246int compare(char *id, char *kw){
247 int len1, len2;
248 len1 = strlen(id);
249 len2 = strlen(kw) - 1;
250
251 if(len1!=len2)
252 return 0; // Different
253 for(int i=0; i<len1; i++){
254 if(id[i]!=kw[i])
255 return 0; // Different
256 }
257 return 1; // Same
258}
259
260int match_word(char *id, char *file){
261 FILE *fptr;
262 int MAX = 100, idx = 1, match = -1;
263
264 fptr = fopen(file, "r");
265 char line[MAX];
266 while( fgets(line,MAX,fptr) ){
267 if(compare(id, line) == 1){
268 match = idx;
269 break;
270 }
271 idx++;
272 }
273 return match;
274}
275
276int main(){
277
278 char id[32];
279 printf("Enter a keyword: ");
280 scanf("%s", id);
281 printf("-----------------------------\n");
282
283 // Check whether Keyword is valid or not
284 int matched = match_word(id, "Keywords.txt");
285 if(matched==-1){
286 printf("ERROR: Invalid Keyword\n");
287 }
288 else{
289 printf("Valid Keyword\n");
290 // Append to Symbol Table if not present
291 int present = match_word(id, "SymbolTableKeyword.txt");
292 if(present!=-1)
293 printf("-> Present at index %d\n", present);
294 else{
295 FILE *fpa = fopen("SymbolTableKeyword.txt","a");
296 fprintf(fpa, "%s\n", id);
297 fclose(fpa);
298 }
299 }
300 return 0;
301}
302
303//Write a program to check whether a given Operator is valid or not. Create a symbol table for the same.
304#include <stdio.h>
305#include <string.h>
306
307int compare(char *id, char *kw){
308 int len1, len2;
309 len1 = strlen(id);
310 len2 = strlen(kw) - 1;
311
312 if(len1!=len2)
313 return 0; // Different
314 for(int i=0; i<len1; i++){
315 if(id[i]!=kw[i])
316 return 0; // Different
317 }
318 return 1; // Same
319}
320
321int match_word(char *id, char *file){
322 FILE *fptr;
323 int MAX = 100, idx = 1, match = -1;
324
325 fptr = fopen(file, "r");
326 char line[MAX];
327 while( fgets(line,MAX,fptr) ){
328 if(compare(id, line) == 1){
329 match = idx;
330 break;
331 }
332 idx++;
333 }
334 return match;
335}
336
337int main(){
338
339 char id[5];
340 printf("Enter an operator: ");
341 scanf("%s", id);
342 printf("-----------------------------\n");
343
344 // Check whether Keyword is valid or not
345 int matched = match_word(id, "Operators.txt");
346 if(matched==-1){
347 printf("ERROR: Invalid Operator\n");
348 }
349 else{
350 printf("Valid Operator\n");
351 // Append to Symbol Table if not present
352 int present = match_word(id, "SymbolTableOperator.txt");
353 if(present!=-1)
354 printf("-> Present at index %d\n", present);
355 else{
356 FILE *fpa = fopen("SymbolTableOperator.txt","a");
357 fprintf(fpa, "%s\n", id);
358 fclose(fpa);
359 }
360 }
361 return 0;
362}
363
364//Write a program in FLEX to check whether a given Identifier is valid or not. Create a symbol table for the same.
365%{
366#undef yywrap
367#define yywrap() 1
368#include <stdio.h>
369 int find_idtf();
370
371 int location = 0;
372 char st[50][10];
373%}
374%option yylineno
375%option noyywrap
376
377%%
378[a-zA-Z]+[a-zA-Z0-9]* {
379 printf("Entered identifier: %s\n", yytext);
380 if(find_idtf(yytext) != -1)
381 printf("Identifier found at index: %d\n", find_idtf(yytext));
382 else{
383 printf("Identifier not found in symbol table. Inserting...\n");
384 //st[location] = yytext;
385 memcpy(st[location], yytext, yyleng);
386 location++;
387 printf("Identifier inserted at index: %d\n", find_idtf(yytext));
388 }
389}
390. {printf("Invalid identifier entered.\n");}
391%%
392
393int find_idtf(char *s){
394 int i;
395 for(i = 0; i < 50; i++){
396 if(strcmp(st[i],s) == 0)
397 return i;
398 }
399 return -1;
400}
401
402int main(){
403 yylex();
404}
405
406//Write a program in FLEX to check whether a given Keyword is valid or not. Create a symbol table for the same.
407%{
408#undef yywrap
409#define yywrap() 1
410#include <stdio.h>
411 int find_idtf();
412
413 int location = 0;
414 char st[50][10];
415%}
416TYPE int|char|bool|float|void|for|do|while|if|else|return|void
417%option yylineno
418%option noyywrap
419
420%%
421{TYPE} {
422 printf("Entered Keyword: %s\n", yytext);
423 if(find_idtf(yytext) != -1)
424 printf("Keyword found at index: %d\n", find_idtf(yytext));
425 else{
426 printf("Keyword not found in symbol table. Inserting...\n");
427 //st[location] = yytext;
428 memcpy(st[location], yytext, yyleng);
429 location++;
430 printf("Keyword inserted at index: %d\n", find_idtf(yytext));
431 }
432}
433. {printf("Invalid keyword entered.\n");}
434%%
435
436int find_idtf(char *s){
437 int i;
438 for(i = 0; i < 50; i++){
439 if(strcmp(st[i],s) == 0)
440 return i;
441 }
442 return -1;
443}
444
445int main(){
446 yylex();
447}
448
449//Write a program in FLEX to check whether a given Operator is valid or not. Create a symbol table for the same.
450%{
451#undef yywrap
452#define yywrap() 1
453#include <stdio.h>
454 int find_idtf();
455
456 int location = 0;
457 char st[50][10];
458%}
459%option yylineno
460%option noyywrap
461
462%%
463[-|+|*|/|%|=|>|<|>=] {
464 printf("Entered Operator: %s\n", yytext);
465 if(find_idtf(yytext) != -1)
466 printf("Operator found at index: %d\n", find_idtf(yytext));
467 else{
468 printf("Operator not found in symbol table. Inserting...\n");
469 //st[location] = yytext;
470 memcpy(st[location], yytext, yyleng);
471 location++;
472 printf("Operator inserted at index: %d\n", find_idtf(yytext));
473 }
474}
475. {printf("Invalid operator entered.\n");}
476%%
477
478int find_idtf(char *s){
479 int i;
480 for(i = 0; i < 50; i++){
481 if(strcmp(st[i],s) == 0)
482 return i;
483 }
484 return -1;
485}
486
487int main(){
488 yylex();
489}
490
491//Implement a sentence parser using YACC for recognizing English Sentences
492
493//Lex Part
494%option c++
495
496%{
497#define IN_EXPR_LEXER
498#include "EngYac.hpp"
499%}
500
501SpaceChar [ \t]
502Space {SpaceChar}+
503SpaceTerm {SpaceChar}|<<EOF>>
504FullStop {SpaceChar}\.{SpaceTerm}
505
506%%
507
508and {return yy::parser::token::Conjunction;}
509or {return yy::parser::token::Conjunction;}
510but {return yy::parser::token::Conjunction;}
511
512the {return yy::parser::token::Article;}
513
514birds {return yy::parser::token::Noun;}
515fish {return yy::parser::token::Noun;}
516C\+\+ {return yy::parser::token::Noun;}
517
518rules {return yy::parser::token::Verb;}
519fly {return yy::parser::token::Verb;}
520swim {return yy::parser::token::Verb;}
521
522{FullStop} {return yy::parser::token::FullStop;}
523
524{Space} {/*Ignore*/}
525. {std::cerr << "Error unknown character\n";}
526
527%%
528
529int yyFlexLexer::yywrap(void)
530{
531 return 1;
532}
533
534
535int yylex(int* /*type*/, yyFlexLexer& lexer)
536{
537 return lexer.yylex();
538}
539
540//YACC part
541%{
542 #ifndef IN_EXPR_LEXER
543 #include <FlexLexer.h>
544 #endif
545 int yylex(int* type, yyFlexLexer& lexer);
546%}
547%skeleton "lalr1.cc"
548%defines
549%parse-param {yyFlexLexer& lexer}
550%lex-param {yyFlexLexer& lexer}
551
552%token Noun
553%token Verb
554%token Article
555%token Conjunction
556%token FullStop
557
558%%
559Input: Sentence FullStop
560
561Sentence: Noun Verb
562 | Article Noun Verb
563 | Sentence Conjunction Sentence
564
565
566%%
567
568void yy::parser::error(yy::location const&, std::string const& msg)
569{
570 std::cerr << "Error: " << msg << "\n";
571}
572
573/*Write LEX and YACC program to parse arithmetic expression for below grammar:
574
575E -> E + E | E - E
576
577E -> E * E | E / E | E ^ E | ( E ) | -E
578
579E -> id
580*/
581//Lex Part
582%{
583#include"y.tab.h"
584extern int yylval;
585%}
586%%
587[0-9]+ {yylval=atoi(yytext); return NUM;}
588\n return 0;
589. return *yytext;
590%%
591
592int yywrap(){
593 return 1;
594}
595//Flex Part
596%{
597#include<stdio.h>
598#include<string.h>
599#include<stdlib.h>
600
601%}
602%token NUM
603%left '+' '-'
604%left '*' '/'
605%right '^'
606%right NEGATIVE
607%%
608S: E {printf("\n");}
609 ;
610E: E '+' E {printf("E->E+E\n");}
611 | E '*' E {printf("E->E*E\n");}
612 | E '-' E {printf("E->E-E\n");}
613 | E '/' E {printf("E->E/E\n");}
614 | '(' E ')'{printf("E->(E)\n");}
615 | E'^'E {printf("E->E^E\n");}
616 | '-' E %prec NEGATIVE {printf("E->-E\n");}
617 | NUM {printf("E->id (%d)\n",yyval);}
618 ;
619%%
620
621int main(){
622 yyparse();
623 printf("\nSTATUS - Valid\n");
624 return 0;
625
626}
627
628int yyerror (char *msg) {
629 return printf ("error YACC: %s\n", msg);
630}
631
632/*Write an YACC program to convert a given expression into its equivalent postfix and prefix expressions.
633
634Ex: (a+b) * (c-d) / e
635*/
636//lex
637%{
638#include"y.tab.h"
639extern int yylval;
640%}
641%%
642[a-z]+ {yylval=(int)yytext[0]; return NUM;}
643\n return 0;
644. return *yytext;
645%%
646
647int yywrap(){
648 return 1;
649}
650//yacc
651%{
652#include<stdio.h>
653#include<string.h>
654#include<stdlib.h>
655
656#define MAX 20
657
658char str[MAX], stack[MAX];
659int top=-1;
660int i=0;
661void push(char c)
662{
663 stack[++top]=c;
664}
665char pop()
666{
667 return stack[top--];
668}
669
670char *strrev(char *str)
671{
672 char *p1, *p2;
673
674 if (! str || ! *str)
675 return str;
676 for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
677 {
678 *p1 ^= *p2;
679 *p2 ^= *p1;
680 *p1 ^= *p2;
681 }
682 return str;
683}
684
685void post_pre()
686{
687 int n,i,j=0; char c[20];
688 char a,b,op;
689 n=strlen(str);
690 for(i=0;i<MAX;i++)
691 stack[i]='\0';
692 for(i=n-1;i>=0;i--)
693 {
694 if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
695 {
696 push(str[i]);
697 }
698 else
699 { c[j++]=str[i];
700 while((top!=-1)&&(stack[top]=='@'))
701 {
702 a=pop(); c[j++]=pop();
703 }
704 push('@');
705 }
706 }
707 c[j]='\0';
708 strrev(c);
709 printf("%s",c);
710}
711
712%}
713%token NUM
714%left '+' '-'
715%left '*' '/'
716%right NEGATIVE
717%%
718S: E ;
719E: E '+' E {str[i]='+';i++;printf("+");}
720 | E '*' E {str[i]='*';i++;printf("*");}
721 | E '-' E {str[i]='-';i++;printf("-");}
722 | E '/' E {str[i]='/';i++;printf("/");}
723 | '(' E ')'
724 | '-' E %prec NEGATIVE {str[i]='-';i++;printf("-");}
725 | NUM {char k;k=yyval;str[i]=k;i++;printf("%c",k);}
726 ;
727%%
728
729int main(){
730 yyparse();
731 printf(" - Postfix Expression\n");
732 str[i]='\0';
733 post_pre(str);
734 printf(" - Prefix expression\n");
735 return 0;
736
737
738}
739
740int yyerror (char *msg) {
741 return printf ("error YACC: %s\n", msg);
742}
743
744
745/*Write a YACC program to check the acceptance of language a^n b^n where n>=0*/
746//lex
747%{
748#include "y.tab.h"
749%}
750
751%%
752[aA] {return A;}
753[bB] {return B;}
754\n {return NL;}
755. {return yytext[0];}
756%%
757//yacc
758%{
759#include<stdio.h>
760#include<stdlib.h>
761%}
762
763%token A B NL
764
765%%
766stmt: S NL {printf("valid string\n");
767 exit(0);}
768;
769S: A S B |
770;
771%%
772
773int yyerror(char *msg)
774{
775printf("invalid string\n");
776exit(0);
777}
778
779main()
780{
781printf("enter the string\n");
782yyparse();
783}
784//Write a program in YACC implementing binary to decimal conversion
785//lex
786%{
787#include<stdio.h>
788#include<stdlib.h>
789#include"y.tab.h"
790extern int yylval;
791%}
792%%
7930 {yylval=0;return ZERO;}
7941 {yylval=1;return ONE;}
795
796[ \t] {;}
797\n return 0;
798. return yytext[0];
799%%
800
801//yacc
802%{
803#include<stdio.h>
804#include<stdlib.h>
805void yyerror(char *s);
806%}
807%token ZERO ONE
808%%
809N: L {printf("\n%d",$$);}
810L: L B {$$=$1*2+$2;}
811| B {$$=$1;}
812B:ZERO {$$=$1;}
813|ONE {$$=$1;};
814%%
815int main()
816{
817while(yyparse());
818}
819yyerror(char *s)
820{
821fprintf(stdout,"\n%s",s);
822}
823
824//Calculate the expression value for a given postfix expression using Yacc compiler
825//lex
826%{
827#include<stdio.h>
828#include"y.tab.h"
829#include<math.h>
830%}
831%%
832[0-9] {yylval.dval=atof(yytext);return NUM;}
833[t];
834n return 0;
835. {return yytext[0];}
836%%
837void yyerror(char * str)
838{
839 printf("n Invalid Expression...");
840}
841int main()
842{
843 yyin=fopen("postfix.txt","r
844 yyparse();
845 return(0);
846}
847//yacc
848%{
849#include<stdio.h>
850int yylex(void);
851float output=0;
852%}
853%union
854{
855 float dval;
856}
857%token <dval> NUMBER
858%left '+' '-'
859%left '*' '/'
860%nonassoc UMINUS
861%type <dval> state
862%type <dval> exp
863%type <dval> N
864%%
865state : exp N {}
866 ;
867exp : NUMBER {$$=$1;output=$$;}
868 | exp exp '+' {$$=$1+$2;output=$$;}
869 | exp exp '-' {$$=$1-$2;output=$$;}
870 | exp exp '*' {$$=$1*$2;output=$$;}
871 | exp exp '/' {$$=$1/$2;output=$$;}
872 ;
873N : {printf("n Output =%fn",output);}
874 ;
875%%
876
877/*Implement the following Parser's using Yacc Compiler
878
879(i) Shift Reduce Parser
880
881(ii) Operator Precedence Parser
882*/
883//operator precedence
884%{
885#include<stdio.h>
886#include<ctype.h>
887%}
888
889%token num
890%left '+''-'
891%left '*' '/'
892%right '^'
893
894%%
895
896s:e'\n'{printf("Value of expression is %d\nAccepted\n",$$);}
897e:e'+'t{printf("reduce E->E+T\n");$$=$1+$3;}
898|t{printf("reduce E->T\n");$$=$1;}
899t:t'*'f{printf("reduce T->T*F\n");$$=$1*$3;}
900|f{printf("reduce T->F\n");$$=$1;}
901f:'('e')'{printf("reduce F->(F)\n");$$=$1;}
902|num{printf("reduce f->id\n");$$=$1;}
903;
904
905%%
906yylex(){
907 int c=getchar();
908 if(isdigit(c)){
909 yylval=c-'0';
910 printf("Shift %c\n",c);
911 return num;
912 }
913 if(c!='\n')
914 printf("Shift %c\n",c);
915 return c;
916}
917int yywrap(){
918 return 1;
919}
920int yyerror(){
921 return 1;
922}
923int main(){
924 printf("Operator precedence parser with * having greater precedence than +\n\n");
925 yyparse();
926 return 1;
927}
928
929//shift reduce
930%{
931#include<stdio.h>
932#include<ctype.h>
933%}
934
935%token num
936%left '+''-'
937%left '*' '/'
938%right '^'
939
940%%
941
942s:exp'\n'{printf("Accepted\n"); return 0;}
943exp:exp'+'exp{printf("reduce e->e+e\n");}
944|exp'*'exp{printf("reduce e->e*e\n");}
945|num{printf("reduce e->id\n");}
946;
947
948%%
949yylex(){
950 int c=getchar();
951 if(isdigit(c)){
952 yylval=c-'0';
953 printf("shift %c\n",c);
954 return num;
955 }
956 if(c!='\n')
957 printf("shift %c\n",c);
958 return c;
959}
960int yywrap(){
961 return 1;
962}
963int yyerror(){
964 return 1;
965}
966int main(){
967 printf("Grammer is:\n");
968 printf("E->E*E\n");
969 printf("E->E+E\n");
970 printf("E->id\n\n");
971 printf("Enter expression\n\n");
972 yyparse();
973 return 1;
974}
975
976/*Implement the following grammar using Yacc Compiler.
977
978E -> E+T/T
979
980T -> T*F/F
981
982F -> (E)/DIGIT
983*/
984
985%{
986#include <stdio.h>
987#include<ctype.h>
988%}
989
990%token DIGIT
991%left '+'
992%left '*'
993
994%%
995
996S: E'\n'{printf("\n%d",$1);}
997E: E'+'T {$$=$1+$3;}
998 |T
999 ;
1000
1001T: T'*'F {$$=$1*$3;}
1002 |F
1003 ;
1004
1005F: '('E')' {$$=$2;}
1006 |DIGIT
1007 ;
1008
1009%%
1010
1011yylex(){
1012 int c=getchar();
1013 if(isdigit(c)){
1014 yylval=c-'0';
1015 return DIGIT;
1016 }
1017 return c;
1018}
1019
1020int yywrap(){
1021 return 1;
1022}
1023
1024int yyerror(){
1025 return 1;
1026}
1027
1028int main(){
1029 yyparse();
1030 printf("\n");
1031 return 1;
1032}
1033
1034/*Implement following grammar using Yacc Compiler
1035
1036E -> E+E
1037
1038E -> E*E
1039
1040E -> (E)/DIGIT
1041*/
1042%token DIGIT
1043%%
1044E : E '+' T
1045 | T
1046 ;
1047T : T '*' F
1048 | F
1049 ;
1050F : '(' E ')'
1051 | DIGIT
1052 ;
1053%%