Last active
September 29, 2020 05:29
-
-
Save blacksmithop/1c98844039eb544f9ea32a423ef60448 to your computer and use it in GitHub Desktop.
lex,yacc program for stack (has bugs) - current code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include "y.tab.h" | |
//extern char * yytext | |
%} | |
%% | |
[0-9]+ {return digit;} | |
[a-z]+ {return id;} | |
[\t] ; | |
[\n] {return 0;} | |
. {return yytext[0];} | |
%% | |
/*int yywrap() | |
{ | |
return 1; | |
}*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include <stdio.h> | |
#include <ctype.h> | |
#include <string.h> | |
int yylex(); | |
void push(); | |
void pop(); | |
void pop2(); | |
void pop3(); | |
void yyerror(); | |
%} | |
%token id digit | |
%left '+''-' | |
%left '*''/' | |
%right '^' | |
%right '=' | |
%nonassoc UMINUS | |
%% | |
S:id {push();}'='E {pop();} | |
E:E'+' {push();}T {pop3();} | |
|E'-'{push();}T {pop3();} | |
|T | |
; | |
T:T'*' {push();}'='F {pop3();} | |
|T'/' {push();}F {pop3();} | |
|E'^'{push();}F {pop3();} | |
|F | |
; | |
F:id {push();} | |
|digit {push();} | |
|'('E')' | |
|'-'{push();}F {pop2();} %prec UMINUS | |
; | |
%% | |
char stack[10]; | |
//char **stack; | |
char var[10],temp[10]; | |
int top=0; | |
char i='0'; | |
int main() | |
{ | |
yyparse(); | |
yylex(); | |
return 0; | |
} | |
void yyerror(char *s) | |
{ | |
printf("%s\n",s); | |
} | |
void push(){ | |
top++; | |
printf("\n%c",yytext); | |
//strcpy(stack[top],yytext[0]); | |
} | |
void pop3() | |
{ | |
printf("\n%c=%s%s%s",i,stack[top-2],stack[top-1],stack[top]); | |
top = top-2; | |
temp[0]=i; | |
strcpy(var,"t"); | |
strcat(var,temp); | |
strcpy(stack[top],var); | |
i++; | |
} | |
void pop() | |
{ | |
printf("\n%s=%s\n", stack[top-1],stack[top]); | |
} | |
void pop2() | |
{ | |
printf("\n%c=%s%s",i,stack[top-1],stack[top]); | |
top--; | |
temp[0]=i; | |
strcpy(var,"t"); | |
strcat(var,temp); | |
strcpy(stack[top],var); | |
i++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment