Calc
Calc
Vedh Bhattad
st.l:
%{
#include <stdio.h>
#include <stdlib.h>
#include "st.tab.h"
extern int yylval;
%}
Num [0-9]+
AO [-+*/%]
PO [()]
%%
{AO} return *yytext;
{PO} return *yytext;
{Num} {yylval=atoi (yytext); return (Num);}
\n return *yytext;
%%
st.y:
%{
#include <stdio.h>
#include "st.tab.h"
int yylex();
void yyerror(char *s);
%}
%token Num
%%
S : E '\n' {printf("%d\n",$1);}
E : E '+' T {$$ = $1 + $3;}
| E '-' T {$$ = $1 - $3;}
| T {$$ = $1;}
T : T '*' F {$$ = $1 * $3;}
| T '/' F {$$ = $1 / $3;}
| T '%' F {$$ = $1 % $3;}
| F {$$ = $1;}
F : '(' E ')' {$$ = $2;}
| Num {$$ = $1;}
%%
int main(){
printf("Enter the expression: ");
yyparse();
return 0;
}
int yywrap(){
return 1;
}
void yyerror(char *s){
fprintf(stderr,"%s\n",s);
}
Output:
st.l:
%{
#include <stdio.h>
#include <stdlib.h>
#include "st.tab.h"
extern int yylval;
%}
Num [0-9]+
AO [-+*/%]
PO [()]
%%
{AO} return *yytext;
{PO} return *yytext;
{Num} {yylval=atoi (yytext); return (Num);}
\n return *yytext;
%%
st.y:
%{
#include <stdio.h>
#include "st.tab.h"
int yylex();
void yyerror(char *s);
%}
%token Num
%%
S : E '\n' {printf("%d\n",$1);}
E : T '+' '+' {$$ = $1 + 1;}
| '+' '+' T {$$ = $3+1;}
| '-' '-' T {$$ = $3-1;}
| T '-' '-' {$$ = $1 -1;}
T : F {$$ = $1;}
F : '(' E ')' {$$ = $2;}
| Num {$$ = $1;}
%%
int main(){
printf("Enter the expression: ");
yyparse();
return 0;
}
int yywrap(){
return 1;
}
void yyerror(char *s){
fprintf(stderr,"%s\n",s);
}
Output:
#include <map>
#include <set>
#include <vector>
#include <string>
if (!isupper(symbol)) { // Terminal
firstSet[nonTerminal].insert(symbol);
break;
} else { // Non-terminal
computeFirst(symbol);
firstSet[nonTerminal].insert(tempFirst.begin(), tempFirst.end());
if (!epsilonFound) break;
if (prod[i] == nonTerminal) {
if (isupper(prod[j])) {
computeFirst(prod[j]);
set<char> tempFirst = firstSet[prod[j]];
followSet[nonTerminal].insert(tempFirst.begin(), tempFirst.end());
if (!epsilonFound) {
allNullable = false;
break;
} else {
followSet[nonTerminal].insert(prod[j]);
allNullable = false;
break;
computeFollow(lhs);
followSet[nonTerminal].insert(followSet[lhs].begin(), followSet[lhs].end());
void generateParsingTable() {
if (!isupper(symbol)) { // Terminal
firstOfProd.insert(symbol);
break;
} else {
firstOfProd = firstSet[symbol];
if (!epsilonFound) break;
if (firstOfProd.count('#')) {
int main() {
int numProductions;
char nonTerminal;
string production;
productions[nonTerminal].push_back(production);
char startSymbol;
computeFirst(entry.first);
computeFollow(entry.first);
generateParsingTable();
cout << "M[" << entry.first.first << ", " << entry.first.second << "] = " << entry.second << "\n";
return 0;
Output:
Code for typecheck:
#include <iostream>
#include <string>
#include <unordered_map>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
if (lhsType == rhsType)
{
cout << "Type of " << lhs << " and " << rhsVar << " -> MATCHED" << endl;
}
else if (lhsType == "NOT DECLARED" || rhsType == "NOT DECLARED")
{
if (lhsType == "NOT DECLARED")
cout << "Type of " << lhs << " -> NOT DECLARED" << endl;
if (rhsType == "NOT DECLARED")
cout << "Type of " << rhsVar << " -> NOT DECLARED" << endl;
}
else
{
cout << "Type of " << lhs << " and " << rhsVar << " -> NOT MATCHED" << endl;
}
}
}
int main()
{
unordered_map<string, string> varTypes;
int n;
cout << "Enter number of lines of code: ";
cin >> n;
cin.ignore();
return 0;
}
Ouput: