Ex4 PDF
Ex4 PDF
Ex4 PDF
l:
%{
#include <stdio.h>
#include <string.h>
#include "subc.tab.h"
char identifier[50][50];
int total_id = 0;
extern YYSTYPE yylval;
int lineNum = 0;
%}
digit [0-9]
letter [a-zA-Z]
mulop [*/%]
addop [+-]
%%{
"if" {
return IF;
}
"else" {
return ELSE;
}
"while" {return WHILE;}
{letter}({letter}|{digit}|"_")?({letter}|{digit}|"_")?({letter}|{digit}|"_")?({letter}|{digit}|"_")?
{
int i;
int flag=0;
for (i = 0; i<total_id; i++) {
if (strcmp(identifier[i], yytext)==0)
{
flag=1;
break;
}
}
//yylval.STRING = malloc(strlen(yytext)+1);
strcpy(yylval.STRING, yytext);
if(flag==0)
{
if(total_id==0) puts("");
//printf("\r%s\n", yylval.string);
//token_number = total_id;
strcpy(identifier[total_id++],yylval.STRING);
}
return ID;
strcpy(yylval.STRING, yytext);
return RELOP;
}
{addop}|("||") {
strcpy(yylval.STRING, yytext);
return ADDOP;
}
{mulop}|("&&") {
strncpy(yylval.STRING, yytext,
strlen(yytext));
return MULOP;
}
"=" {return ASSIGN;}
";" {return END;}
"!" {return NOT;}
"[" {return SQRBO;}
"]" {return SQRBC;}
"(" {return STRBO;}
")" {return STRBC;}
"{" {return CURBO;}
"}" {return CURBC;}
"\n" {lineNum++;}
[._]+ {yyerror("Invalid character");}
[ \t]+;
%%
subc.y:
%{
#include<stdio.h>
#include<string.h>
int ErrorRecovered = 0;
char message[100];
char idflag[50][50];
int nModId = 0;
struct QUAD{
char operation[10];
char arg1[20];
char arg2[20];
char result[10];
}quads[100];
int Q = 0, T = 0;
int L = 0;
int LABEL_STACK[100];
int LABEL_STACK_TOP=0;
char STMT_STACK[100][100];
int STACK_TOP = 0;
int total_symbol=0;
int i;
for(i=0;i<total_symbol;i++){
if(strcmp(symbol,SYMBOL_TABLE[i].symbol)==0){
SYMBOL_TABLE[i].value = value;
return;
}
}
int IF_NO=1;
%}
%union{
float VALUE;
char STRING[100];
}
%start Start
%token ID NUM RELOP ADDOP MULOP ASSIGN NOT SQRBO SQRBC STRBO STRBC END NEWLINE CURBO
CURBC IF ELSE WHILE
%%
Start : Stmt_list
{
int i;
for(i=0;i<STACK_TOP;i++){
printf("\r%s\n", STMT_STACK[i]);
}
};
Stmt_list : Stmt
{
strcpy(message, "Missing expression!");
}
| Stmt_list Stmt
{
strcpy(message,"It's not the last line of the file!");
};
Stmt : Variable ASSIGN Expression END
{
char str[100], result[100], num[100];
strcpy(num, $3);
strcpy(result, $1);
strcpy(quads[Q].operation, $2);
strcpy(quads[Q].arg1, "");
strcpy(quads[Q].arg2, num);
strcpy(quads[Q].result, result);
Q++;
SET_SYMBOL_VALUE(result, Num);
strcpy(num, $3);
}
CURBO Stmt_list CURBC
{
char str[100],label[100];
| WHILE
{
char str[100],label[100];
IF_NO = 1;
strcpy(num, $4);
sprintf(str, "if %s is false goto %s", num,label);
STMT_STACK_push(str);
}
CURBO Stmt_list CURBC
{
char str[100],label[100];
strcpy(label, "label"); sprintf(str, "%d", L-IF_NO);
strcat(label,str);
sprintf(str, "goto %s", label);
STMT_STACK_push(str);
}
;
Variable : ID
{
strcpy($$,$1);
GET_SYMBOL_VALUE($1);
Expression : Simple_expression
{
strcpy($$, $1);
strcpy(message,"Missing expression!");
}
| Simple_expression RELOP Simple_expression
{
strcpy($$, str);
GET_SYMBOL_VALUE(str);
SET_SYMBOL_VALUE(str, CALCULATE(Num1, Num2,
$2));
Simple_expression: Term
{
strcpy($$, $1);
}
| Simple_expression ADDOP Term
{
char str[100], result[100], num1[100], num2[100];
strcpy(result, "T"); sprintf(str, "%d", ++T);
strcat(result,str);
strcpy(num1, $1);
strcpy(num2, $3);
strcpy(quads[Q].operation, $2);
strcpy(quads[Q].arg1, num1);
strcpy(quads[Q].arg2, num2);
strcpy(quads[Q].result, result);
Q++;
GET_SYMBOL_VALUE(result);
SET_SYMBOL_VALUE(result, Result);
strcpy($$, result);
Term : Factor
{
strcpy($$, $1);
}
| Term MULOP Factor
{
strcpy(num1, $1);
strcpy(num2, $3);
strcpy(quads[Q].operation, $2);
strcpy(quads[Q].arg1, num1);
strcpy(quads[Q].arg2, num2);
strcpy(quads[Q].result, result);
Q++;
strcpy($$, result);
Factor : ID
{
strcpy($$,$1);
GET_SYMBOL_VALUE($1);
| NUM
{
char str[100], result[100], num[100];
strcpy(result, "T"); sprintf(str, "%d", ++T);
strcat(result,str);
strcpy(quads[Q].operation, "");
strcpy(quads[Q].arg1, "");
strcpy(quads[Q].arg2, num);
strcpy(quads[Q].result, result);
Q++;
GET_SYMBOL_VALUE(result);
SET_SYMBOL_VALUE(result, $1);
}
| STRBO Expression STRBC
{
strcpy(message,"'(' or ')' missing OR expression not
found!");
}
| NOT Factor
{
char str[100], result[100], num[100];
strcpy(result, "T"); sprintf(str, "%d", ++T);
strcat(result,str);
strcpy(quads[Q].operation, "!");
strcpy(quads[Q].arg1, "");
strcpy(quads[Q].arg2, num);
strcpy(quads[Q].result, result);
Q++;
strcpy($$,result);
}
;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("input.txt","r");
yyparse();
fclose(yyin);
if(ErrorRecovered==0) printf("Success!\n");
return 0;
}
Input.txt:
b = b + 1;
if(b > a){b = 2 + c;} else{b = d - 2;}
a = c + b * d;
Output: