⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 solution7.y.bak

📁 lex files for given decription used as assignment in compiler design
💻 BAK
字号:
/*  Kevindra singh RIT2006025	*/
%{ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
extern int yylex();

#define YYERROR_VERBOSE

void yyerror(const char *msg)
{
      printf("ERROR(PARSER): %s\n", msg);
}
char temp[MAX][MAX];
char index;
%}

%union{
       char ch;
       int index;
}

%type <index> expr
%type <index> term
%type <index> fact

%token <ch> NAME

%%

statementlist : statement '\n' 
              | statement '\n' statementlist
              ;

statement : expr              { printf("%s\n", temp[$1]); }
           ; 

expr: expr '+' term     { $$ = $1; strcat(temp[$1],temp[$3]); strcat(temp[$1],"+"); }
          | expr '-' term     { $$ = $1;strcat(temp[$1],temp[$3]); strcat(temp[$1],"-"); }
          | term                    { $$ = $1; }
          ;

term : term '*' factor            { $$ = $1;strcat(temp[$1],temp[$3]);strcat(temp[$1],"*");  }
     | term '/' factor            { $$ = $1;strcat(temp[$1],temp[$3]);strcat(temp[$1],"/"); 
	                            }
     | factor                   { $$ = $1; }
     ;

factor   : NAME                  { $$=index; temp[index][0]=$1; temp[index][1]='\0'; index++; }
         | '(' expr ')'    { $$ = $2; }
         ;

%%

main()
{
        int i;
       index=0;
       yyparse();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -