Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define LETTER 256
- int symbol;
- char lexVal;
- int yylex();
- typedef struct _node
- {
- char key;
- struct _node* left;
- struct _node* right;
- }node;
- node* E();
- node* T();
- node* F();
- node* P();
- void error(){
- printf("eroare de sintaxa");
- exit(0);
- }
- void next_symbol()
- {
- symbol = yylex();
- }
- node* E()
- {
- node* resultT=T();
- if(symbol == '|')
- {
- next_symbol();
- node* result = (node*)malloc(sizeof(node));
- result->key = '|';
- result->left=resultT;
- result->right =E();
- return result;
- }
- if(symbol == '\n'||symbol==')')
- return resultT;
- error();
- }
- int followF(){ return firstT()||followT();}
- node* T(){
- node* resultF;
- resultF = F();
- if(symbol=='(' || symbol==LETTER)
- {
- node* result;
- result = (node*)malloc(sizeof(node));
- return resultT;
- result->left=resultF;
- result->right =T();
- return result;
- }
- return resultF;
- }
- node* F(){
- node* resultP=P();
- if (symbol == '*'){
- next_symbol();
- node* result = (node*)malloc(sizeof(node));
- result->key = '*';
- result->left=resultP;
- result->right =NULL;
- return result;
- }
- return resultP;
- }
- node* P(){
- node* val;
- if (symbol==LETTER) {
- val=(node*)malloc(sizeof(node));
- val->key = lexval;
- val->left=NULL;
- val->right=NULL;
- next_symbol();
- return val;
- }
- else if (symbol=='('){
- next_symbol();
- val = E();
- if (symbol==')') next_symbol();
- else error();
- return val;
- } else error();
- }
- parser(){
- printf("%d\n", E()->key);
- if (symbol!='\n') error();
- }
- main(){
- next_symbol();
- while (symbol!=0){
- parser();
- next_symbol();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement