编译原理课程设计_C--编译器_语法分析&代码生成 - Justin

编译原理课程设计--C语言编译器 编译原理课程设计--C语言编译器 代码可输出 action-goto表 源程序 四元式 优化后的四元式 语法分析结果 词法分析结果 块的信息等 阅读详情
导读:
  %{
  #include "ctype.h"
  #include "string.h"
  #include "stdio.h"
  #include "stdlib.h"
  #define HASHSIZE 256
  #define GLOBAL 0
  typedef int Type;
  typedef struct symbol *Symbol;
  typedef struct table *Table;
  Table types;
  Table currentTable;
  int currentLevel;
  int params;//堆栈已分配的单元个数;
  int paramcount;//参数和临时变量总数记录
  int realParam;//参数的个数记录
  int calltype=0;//函数调用方式__stdcall&__cdecl
  char funBuffer[1024];//函数声明缓冲区
  char localValBuffer[1024]; //局部变量缓冲区
  char varBuffer[1024];//全局变量缓冲区
  //char stringBuffer[1024];//字符串常量缓冲区
  int msgIndex = 0;//字符串索引
  int bufferLen;//缓冲区长度
  int callparam;//调用时记录参数数目
  int label=0;//自动生成标号
  struct symbol {
  char *name;//串名
  int scope;//层次
  int sclass;//符号表内类型
  Symbol up;//上一个被插入的符号的指针
  int ty;//类型
  float ref;//引用地址
  int value;//符号表中符号的值
  int calltype;//如果是函数,记录调用方式
  int isLocal;//记录是全局变量还是局部变量
  };
  /*
  void 1 int 2 char 3
  void* 11 int* 12 char* 13
  void[] 21 int[] 22 char[] 23
  void*[] 31 int*[] 32 char*[] 33
  */
  struct entry {
  struct symbol sym;
  struct entry *link;
  };
  struct table {
  int level;
  Table previous;
  struct entry *buckets[HASHSIZE];
  Symbol all;
  };
  Table table( Table tp, int level);
  Symbol install(char *name, Table *tpp, int level, int sclass);
  Symbol lookup(char *name, Table tp);
  void enterscope();
  void exitscope();
  Table table( Table tp, int level)
  {
  int i;
  Table newTemp = (Table) malloc( sizeof( struct table ) );
  newTemp->previous = tp;
  newTemp->level = level;
  for( i = 0; i   {
  newTemp->buckets[i] = NULL;
  }
  if (tp)
  newTemp->all = tp->all;
  return newTemp;
  }
  Symbol install(char *name, Table *tpp, int level, int sclass)
  {
  Table tp = *tpp;
  struct entry *p;
  int len;
  unsigned h = name[0] &(HASHSIZE - 1);
  if (level >0 &&tp->level   tp = *tpp = table(tp, level);
  currentTable = tp;
  p = (struct entry *) malloc( sizeof( struct entry ) );
  len = strlen( name );
  p->sym.name = (char *) malloc( len * sizeof( char ) );
  strcpy( p->sym.name, name );
  p->sym.scope = level;
  p->sym.up = tp->all;
  p->sym.sclass = sclass;
  tp->all = &p->sym;
  p->link = tp->buckets[h];
  tp->buckets[h] = p;
  return &p->sym;
  }
  Symbol lookup(char *name, Table tp) {
  struct entry *p;
  unsigned h = name[0] &(HASHSIZE-1);
  if( tp == NULL ) return NULL;
  do
  for (p = tp->buckets[h]; p; p = p->link )
  {
  if ( p != NULL &&strcmp(name, p->sym.name) == 0 )
  return &p->sym;
  }
  while ((tp = tp->previous) != NULL);
  return NULL;
  }
  void enterscope() {
  currentLevel++;
  }
  void exitscope() {
  if (currentTable->level == currentLevel) {
  Table tt = currentTable;
  currentTable = currentTable->previous;
  free( tt );
  }
  --currentLevel;
  }
  Symbol makeSym( char *text )
  {
  int len;
  Symbol temp = (Symbol) malloc( sizeof( struct symbol ) );
  len = strlen( text );
  temp->name = (char *) malloc( len * sizeof( char ) );
  strcpy( temp->name, text );
  temp->scope = currentLevel;
  return temp;
  }
  typedef struct {//表达式的类型
  int is_const;
  int value;
  char *code;
  char *name;
  char *temp;
  } expr;
  %}
  %token ID NUM CHARACTER STRING INT VOID CHAR IF ELSE
  WHILE RETURN __CDECL __STDCALL LE LT GT GE EQ
  NE OR AND INT VOID CHAR FUNCTION ARRAY POINTER NEG
  %start program
  %union{ int* value;
  Symbol symType;
  expr* exprType;
  char* op;
  };
  %token <symType> ID
  %token <symType> NUM
  %token <symType> STRING
  %token <symType> CHARACTER
  %type <exprType> param
  %type <exprType> program
  %type <exprType> startstmt
  %type <exprType> declaration_list
  %type <exprType> declaration
  %type <value> type_specifer
  %type <exprType> params
  %type <exprType> compound_stmt
  %type <exprType> param_list
  %type <exprType> local_declarations
  %type <exprType> var_declaration
  %type <exprType> fun_declaration
  %type <exprType> statement_list
  %type <exprType> statement
  %type <exprType> expression_stmt
  %type <exprType> if_stmt
  %type <exprType> while_stmt
  %type <exprType> return_stmt
  %type <exprType> expression
  %type <exprType> simple_expression
  %type <exprType> additive_expression
  %type <exprType> var
  %type <exprType> relop
  %type <exprType> term
  %type <op> mulop
  %type <exprType> unary_expression
  %type <op> unaryop
  %type <exprType> factor
  %type <exprType> call
  %type <exprType> args
  %type <exprType> arg_list
  %type <value> calling_convention
  %type <exprType> loc_declaration
  %type <op> addop
  %right '='
  %left '+' '-'
  %left '*' '/' '%'
  %%
  program : declaration_list
  {
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = ( char * ) malloc( strlen( $1->code ) + 100 );
  sprintf( $$->code, "%sEND main/n", $1->code );
  fprintf( yyout, ".386/n.MODEL flat, stdcall/n.Stack 4096/n" );
  fprintf( yyout, "/n%s", funBuffer );
  fprintf( yyout, "/n.data/n%s", varBuffer );
  fprintf( yyout, "/n.code/n%s", $$->code );
  }
  ;
  declaration_list : declaration_list declaration
  {
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = ( char * ) malloc( strlen( $1->code ) + strlen( $2->code ) + 1 );
  sprintf( $$->code, "%s%s", $1->code, $2->code );
  }
  | declaration
  {
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = ( char * ) malloc( strlen( $1->code ) + 1 );
  sprintf( $$->code, "%s", $1->code );
  }
  ;
  declaration : var_declaration
  {
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = ( char * ) malloc( strlen( $1->code ) + 1 );
  sprintf( $$->code, "");
  sprintf( varBuffer, "%s%s", varBuffer, $1->code );
  }
  | fun_declaration
  {
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = ( char * ) malloc( strlen( $1->code ) + 10 );
  sprintf( $$->code, "%s", $1->code );
  }
  ;
  var_declaration : type_specifer ID ';'
  {
  if( lookup( $2->name, currentTable ) == NULL )
  {
  $2 = install($2->name, ¤tTable, currentLevel, ID);
  $2->isLocal = 0;
  }
  else
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $2->name );
  yyerror( msg );
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $2->ty = 1;
  yyerror( "Type Error: ");
  break;
  case 2:
  $2->ty = 2;
  sprintf( $$->code, " %s DWORD 0/n", $2->name );
  break;
  case 3:
  $2->ty = 3;
  sprintf( $$->code, " %s BYTE 0/n", $2->name );
  break;
  }
  }
  | type_specifer ID '[' NUM ']' ';'
  {
  if( lookup( $2->name, currentTable ) == NULL )
  {
  $2 = install($2->name, ¤tTable, currentLevel, ID);
  $2->isLocal = 0;
  }
  else
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $2->name );
  yyerror( msg );
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->name ) + strlen( $4->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $2->ty = 21;
  yyerror( "Type Error: ");
  break;
  case 2:
  $2->ty = 22;
  sprintf( $$->code, " %s DWORD %s DUP(0)/n", $2->name, $4->name );
  break;
  case 3:
  $2->ty = 23;
  sprintf( $$->code, " %s BYTE %s DUP(0)/n", $2->name, $4->name );
  break;
  }
  }
  | type_specifer '*' ID ';'
  {
  if( lookup( $3->name, currentTable ) == NULL )
  {
  $3 = install($3->name, ¤tTable, currentLevel, ID);
  $3->isLocal = 0;
  }
  else
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $3->name );
  yyerror( msg );
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $3->ty = 11;
  yyerror( "Type Error: ");
  break;
  case 2:
  $3->ty = 12;
  sprintf( $$->code, " %s PTR DWORD 0/n", $3->name );
  break;
  case 3:
  $3->ty = 13;
  sprintf( $$->code, " %s PTR BYTE 0/n", $3->name );
  break;
  }
  }
  | type_specifer '*' ID '[' NUM ']' ';'
  {
  if( lookup( $3->name, currentTable ) == NULL )
  {
  $3 = install($3->name, ¤tTable, currentLevel, ID);
  $3->isLocal = 0;
  }
  else
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $3->name );
  yyerror( msg );
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->name ) + strlen( $5->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $3->ty = 31;
  yyerror( "Type Error: ");
  break;
  case 2:
  $3->ty = 32;
  sprintf( $$->code, " %s PTR DWORD %s DUP(0)/n", $3->name, $5->name );
  break;
  case 3:
  $3->ty = 33;
  sprintf( $$->code, " %s PTR BYTE %s DUP(0)/n", $3->name, $5->name );
  break;
  }
  }
  ;
  loc_declaration : type_specifer ID ';'
  {
  Symbol temp;
  if( (temp = lookup( $2->name, currentTable )) == NULL )
  {
  $2 = install($2->name, ¤tTable, currentLevel, ID);
  $2->isLocal = 1;
  }
  else
  {
  if( temp->scope == currentLevel )
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $2->name );
  yyerror( msg );
  }
  else
  {
  $2 = install($2->name, ¤tTable, currentLevel, ID);
  $2->isLocal = 1;
  }
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $2->ty = 1;
  yyerror( "Type Error: ");
  break;
  case 2:
  $2->ty = 2;
  sprintf( localValBuffer, "%s/tlocal %s:DWORD/n", localValBuffer, $2->name );
  break;
  case 3:
  $2->ty = 3;
  sprintf( localValBuffer, "%s/tlocal %s:DWORD/n", localValBuffer, $2->name );
  break;
  }
  sprintf( $$->code, "");
  paramcount++;
  realParam++;
  }
  | type_specifer ID '[' NUM ']' ';'
  {
  Symbol temp;
  if( (temp = lookup( $2->name, currentTable )) == NULL )
  {
  $2 = install($2->name, ¤tTable, currentLevel, ID);
  $2->isLocal = 1;
  }
  else
  {
  if( temp->scope == currentLevel )
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $2->name );
  yyerror( msg );
  }
  else
  {
  $2 = install($2->name, ¤tTable, currentLevel, ID);
  $2->isLocal = 1;
  }
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->name ) + strlen( $4->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $2->ty = 21;
  yyerror( "Type Error: ");
  break;
  case 2:
  $2->ty = 22;
  sprintf( localValBuffer, "%s/tlocal %s[%s]:DWORD/n", localValBuffer, $2->name, $4->name );
  break;
  case 3:
  $2->ty = 23;
  sprintf( localValBuffer, "%s/tlocal %s[%s]:DWORD/n", localValBuffer, $2->name, $4->name );
  break;
  }
  sprintf( $$->code, "");
  paramcount++;
  realParam++;
  }
  | type_specifer '*' ID ';'
  {
  Symbol temp;
  if( (temp = lookup( $3->name, currentTable )) == NULL )
  {
  $3 = install($3->name, ¤tTable, currentLevel, ID);
  $3->isLocal = 1;
  }
  else
  {
  if( temp->scope == currentLevel )
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $3->name );
  yyerror( msg );
  }
  else
  {
  $3 = install($3->name, ¤tTable, currentLevel, ID);
  $3->isLocal = 1;
  }
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $3->ty = 11;
  yyerror( "Type Error: ");
  break;
  case 2:
  $3->ty = 12;
  sprintf( localValBuffer, "/tlocal %s:PTR WORD/n", $3->name );
  break;
  case 3:
  $3->ty = 13;
  sprintf( localValBuffer, "/tlocal %s:PTR BYTE/n", $3->name );
  break;
  }
  sprintf( $$->code, "");
  paramcount++;
  realParam++;
  }
  | type_specifer '*' ID '[' NUM ']' ';'
  {
  Symbol temp;
  if( (temp = lookup( $3->name, currentTable )) == NULL )
  {
  $3 = install($3->name, ¤tTable, currentLevel, ID);
  $3->isLocal = 1;
  }
  else
  {
  if( temp->scope == currentLevel )
  {
  char msg[100];
  sprintf( msg, "Variable %s redefinition: ", $3->name );
  yyerror( msg );
  }
  else
  {
  $3 = install($3->name, ¤tTable, currentLevel, ID);
  $3->isLocal = 1;
  }
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->name ) + strlen( $5->name ) + 13 );
  switch( *$1 )
  {
  case 1:
  $3->ty = 31;
  yyerror( "Type Error: ");
  break;
  case 2:
  $3->ty = 32;
  sprintf( localValBuffer, "/tlocal %s[%s]:DWORD/n", $3->name, $5->name );
  break;
  case 3:
  $3->ty = 33;
  sprintf( localValBuffer, "/tlocal %s[%s]:DWORD/n", $3->name, $5->name );
  break;
  }
  sprintf( $$->code, "");
  paramcount++;
  realParam++;
  }
  ;
  type_specifer : INT
  {
  $$ = (int*)malloc( sizeof( int ) );
  *$$ = 2;
  }
  | VOID
  {
  $$ = (int*)malloc( sizeof( int ) );
  *$$ = 1;
  }
  | CHAR
  {
  $$ = (int*)malloc( sizeof( int ) );
  *$$ = 3;
  }
  ;
  fun_declaration : type_specifer calling_convention ID '(' params ')' compound_stmt
  {
  
  // if( lookup( $3->name, currentTable ) == NULL )
  // {
  // $3 = install($3->name, ¤tTable, currentLevel, ID);
  // }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = ( char * ) malloc( strlen( $3->name )*2 + strlen( $5->code ) + strlen( $7->code ) + strlen( localValBuffer ) + 12 );
  if( *$2== 0 &¶ms >0 )
  {
  // $3->calltype = 0;
  sprintf( $$->code, "%s PROC%s/n%s/tsub esp, %d/n%s/tret %d/n%s ENDP/n", $3->name, $5->code, $7->code, localValBuffer, (paramcount-realParam)*4, 4*params, $3->name );
  }
  else
  {
  // $3->calltype = 1;
  sprintf( $$->code, "%s PROC%s/n%s/tsub esp, %d/n%s/tret/n%s ENDP/n", $3->name, $5->code, $7->code, localValBuffer, (paramcount-realParam)*4, $3->name );
  }
  params = 0;
  paramcount = 0;
  realParam = 0;
  sprintf( localValBuffer, "");
  }
  | type_specifer calling_convention ID '(' params ')' ';'
  {
  if( lookup( $3->name, currentTable ) == NULL )
  {
  $3 = install($3->name, ¤tTable, currentLevel, ID);
  }
  else
  {
  char msg[100];
  sprintf( msg, "The Function %s redefinition: ", $3->name );
  yyerror( msg );
  }
  if( *$2== 0 &¶ms >0 )
  {
  $3->calltype = 0;
  }
  else
  {
  $3->calltype = 1;
  }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = "";
  sprintf( funBuffer, "%s PROTO%s/n", $3->name, $5->code );
  }
  | type_specifer ID '(' params ')' compound_stmt
  {
  // if( lookup( $2->name, currentTable ) == NULL )
  // {
  // $2 = install($2->name, ¤tTable, currentLevel, ID);
  // }
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = ( char * ) malloc( strlen( $2->name ) * 2 + strlen( $4->code ) + strlen( $6->code ) + strlen( localValBuffer ) + 20 );
  // $2->calltype = 0;
  if( params >0 )
  {
  sprintf( $$->code, "%s PROC%s/n%s/tsub esp, %d/n%s/tret %d/n%s ENDP/n", $2->name, $4->code, localValBuffer, (paramcount-realParam)*4, $6->code, 4*params, $2->name );
  }
  else
  {
  sprintf( $$->code, "%s PROC%s/n%s/tsub esp, %d/n%s/tret/n%s ENDP/n", $2->name, $4->code, localValBuffer, (paramcount-realParam)*4, $6->code, $2->name );
  }
  params = 0;
  paramcount = 0;
  realParam = 0;
  sprintf( localValBuffer, "");
  }
  | type_specifer ID '(' params ')' ';'
  {
  if( lookup( $2->name, currentTable ) == NULL )
  {
  $2 = install($2->name, ¤tTable, currentLevel, ID);
  }
  else
  {
  char msg[100];
  sprintf( msg, "The Function %s redefinition: ", $2->name );
  yyerror( msg );
  }
  $2->calltype = 0;
  $$ = ( expr * ) malloc( sizeof( expr ) );
  $$->code = "";
  sprintf( funBuffer, "%s%s PROTO%s/n", funBuffer, $2->name, $4->code );
  }
  ;
  params : param_list
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = $1->code;
  }
  | VOID
  {
  params = 0;
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = "";
  }
  ;
  param_list : param_list ',' param
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + strlen( $3->code ) + 1 );
  sprintf( $$->code, "%s%s", $1->code, $3->code );
  params++;
  }
  | param
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = $1->code;
  params = 1;
  }
  ;
  param : type_specifer ID
  {
  Symbol temp = install($2->name, ¤tTable, currentLevel+1, ID);
  $$->code = (char *) malloc( 100 );
  switch( *$1 )
  {
  case 1:
  temp->ty = 1;
  yyerror( "Parameters Type Error: ");
  break;
  case 2:
  temp->ty = 2;
  sprintf( $$->code, ", %s:DWORD", $2->name );
  break;
  case 3:
  temp->ty = 3;
  sprintf( $$->code, ", %s:DWORD", $2->name );
  break;
  default:
  break;
  }
  }
  | type_specifer '*' ID
  {
  Symbol temp = install($3->name, ¤tTable, currentLevel+1, ID);
  $$->code = (char *) malloc( 100 );
  switch( *$1 )
  {
  case 1:
  temp->ty=11;
  sprintf( $$->code, ", %s:PTR DWORD", $3->name );
  break;
  case 2:
  temp->ty=12;
  sprintf( $$->code, ", %s:PTR WORD", $3->name );
  break;
  case 3:
  temp->ty=13;
  sprintf( $$->code, ", %s:PTR BYTE", $3->name );
  break;
  }
  }
  | type_specifer ID '[' ']'
  {
  Symbol temp = install($2->name, ¤tTable, currentLevel+1, ID);
  $$->code = (char *) malloc( 100 );
  switch( *$1 )
  {
  case 1:
  temp->ty = 21;
  yyerror( "Parameters Type Error: ");
  break;
  case 2:
  temp->ty = 22;
  sprintf( $$->code, ", %s:PTR WORD", $2->name );
  break;
  case 3:
  temp->ty = 23;
  sprintf( $$->code, ", %s:PTR BYTE", $2->name );
  break;
  }
  }
  | type_specifer '*' ID '[' ']'
  {
  Symbol temp = install($3->name, ¤tTable, currentLevel+1, ID);
  $$->code = (char *) malloc( 100 );
  switch( *$1 )
  {
  case 1:
  temp->ty = 31;
  sprintf( $$->code, ", %s:DWORD", $3->name );
  break;
  case 2:
  temp->ty = 32;
  sprintf( $$->code, ", %s:DWORD", $3->name );
  break;
  case 3:
  temp->ty = 33;
  sprintf( $$->code, ", %s:DWORD", $3->name );
  break;
  }
  
  }
  ;
  compound_stmt : '{' local_declarations statement_list '}'
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->code ) + strlen( $3->code ) + 12 );
  sprintf( $$->code, "%s%s", $2->code, $3->code );
  }
  ;
  local_declarations : /*空值*/
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = "";
  }
  | local_declarations loc_declaration
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + strlen( $2->code ) + 1 );
  sprintf( $$->code, "%s%s", $1->code, $2->code );
  }
  ;
  statement_list : /*空值*/
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 2 );
  $$->code = "";
  }
  | statement_list statement
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + strlen( $2->code ) + 1 );
  sprintf( $$->code, "%s%s", $1->code, $2->code );
  }
  ;
  statement : expression_stmt
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code )+20 );
  sprintf( $$->code, "%s/n", $1->code );
  }
  | compound_stmt
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = $1->code;
  }
  | if_stmt
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = $1->code;
  }
  | while_stmt
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = $1->code;
  }
  | return_stmt
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = $1->code;
  }
  ;
  expression_stmt : expression ';'
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  //$$->code = (char *) malloc( strlen( $1->code )+20 );
  $$->code= $1->code;
  }
  | ';'
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 2 );
  sprintf( $$->code, "");
  }
  ;
  if_stmt : IF '(' expression ')' statement
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->code ) + strlen( $5->code ) + strlen( $3->temp ) + 30 );
  sprintf( $$->code, "%s/t.IF %s/n%s/t.ENDIF/n", $3->temp, $3->code, $5->code );
  }
  | IF '(' expression ')' statement ELSE statement
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->code ) + strlen( $5->code ) + strlen( $3->temp ) + strlen( $7->code ) + 10 );
  sprintf( $$->code, "%s/t.IF %s/n%s/t.ELSE/n%s/t.ENDIF/n", $3->temp, $3->code, $5->code, $7->code );
  }
  ;
  while_stmt : WHILE '(' expression ')' statement
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->temp ) + strlen( $3->code ) + strlen( $5->code ) + strlen($3->temp) + 10 );
  sprintf( $$->code, "_label%d:/n%s/t.IF !( %s ) /n/tjmp _label%d/n/t.ENDIF/n%s/tjmp _label%d/n_label%d:/n", label, $3->temp, $3->code,label+1, $5->code, label, label + 1 );
  label += 2;
  }
  ;
  return_stmt : RETURN ';'
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 15 );
  sprintf( $$->code, "/tmov eax, 0/n/tret/n" );
  }
  | RETURN expression ';'
  {
  $$ = (expr*) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->name ) + strlen( $2->code ) + 15 );
  sprintf( $$->code, "%s/tmov eax, %s/n/tret/n", $2->code, $2->name );
  }
  ;
  expression : var '=' expression
  {
  
  if( $1->value == 0 )
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + strlen( $3->code )+ strlen( $3->name ) + 10 );
  sprintf( $$->code, "%s/tmov eax, %s/n/tmov %s, eax/n", $3->code, $3->name, $1->code );
  $$->name = (char *)malloc( sizeof( $1->name ) );
  sprintf( $$->name, "%s", $1->name );
  $$->temp = $$->code;
  }
  else
  {
  
  char *s = (char *) malloc( strlen( $1->name ) + strlen( $1->code ) + strlen( $3->code )+ strlen( $3->name ) + 100 );
  $$ = (expr *) malloc( sizeof( expr ) );//sprintf( $$->code, "%s/tmov eax, %s/n%s/tmov %s, eax/n", $3->code, $3->name, $1->code, $1->name );
  sprintf( s, "%s/tmov eax, ", $3->code );
  sprintf( s, "%s%s/n", s, $3->name );
  sprintf( s, "%s%s/tmov ", s, $1->code );
  sprintf( s, "%s%s, eax/n", s, $1->name );
  $$->code = (char *) malloc( strlen( $1->name ) + strlen( $1->code ) + strlen( $3->code )+ strlen( $3->name ) + 100 );
  $$->code = s;
  $$->name = (char *)malloc( sizeof( $1->code ) );
  sprintf( $$->name, "%s", $1->code );
  $$->temp = $$->code;
  
  }
  }
  | simple_expression
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + 1 );
  $$->name = $1->name;
  sprintf( $$->code, "%s", $1->code );
  $$->temp = $1->temp;
  }
  ;
  var : ID
  {
  if( lookup( $1->name, currentTable ) == NULL )
  {
  char msg[100];
  sprintf( msg, "Variable %s haven't been defined: ", $1->name );
  yyerror( msg );
  }
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->name ) + 1 );
  sprintf( $$->code, "%s", $1->name );
  $$->is_const = 1;
  $$->name = $1->name;
  $$->value = 0;
  }
  | ID '[' expression ']'
  {
  Symbol temp;
  if( temp = lookup( $1->name, currentTable ) == NULL )
  {
  char msg[100];
  sprintf( msg, "Variable %s haven't been defined: ", $1->name );
  yyerror( msg );
  }
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->code ) + strlen( $3->name ) + 10 );
  sprintf( $$->code, "%s/tmov edi, %s/n", $3->code, $3->name );
  $$->name = (char *)malloc( sizeof( $1->name ) );
  sprintf( $$->name, "DWORD ptr %s[edi]", $1->name );
  $$->temp = $$->code;
  $$->value = 1;
  }
  | '*' ID
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->name ) + 10 );
  sprintf( $$->code, "/tmov esi, %s/n", $2->name );
  $$->name = (char *)malloc( sizeof( 6 ) );
  sprintf( $$->name, "DWORD PTR [esi]" );
  $$->value = 1;
  }
  ;
  simple_expression : additive_expression relop additive_expression
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $3->name ) + strlen( $2->code ) + 5 );
  sprintf( $$->code, "edx %s %s", $2->code, $3->name );
  $$->temp = (char *) malloc( strlen( $1->code ) +strlen( $1->code ) + strlen( $3->code ) + 20 );
  sprintf( $$->temp, "%s%s/tmov edx, %s/n", $3->code, $1->code, $1->name );
  }
  | additive_expression
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->value = $1->value;
  $$->code = $1->code;
  $$->name = $1->name;
  }
  ;
  relop : LE
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 3 );
  sprintf( $$->code, "<=" ); <br >  }
  | LT
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 2 );
  sprintf( $$->code, "<"); <br >  }
  | GT
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 2 );
  sprintf( $$->code, ">");
  }
  | GE
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 3 );
  sprintf( $$->code, ">=" );
  }
  | EQ
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 3 );
  sprintf( $$->code, "==" );
  }
  | NE
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 3 );
  sprintf( $$->code, "!=" );
  }
  ;
  additive_expression : additive_expression addop term
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  switch( *$2 )
  {
  case '+':
  $$->code = (char *) malloc( strlen($1->code) + strlen($1->name) + strlen($3->name) + strlen($3->code) + 1024 );
  sprintf( $$->code, "%s%s/tmov eax, %s/n/tadd eax, %s/n", $1->code, $3->code, $1->name, $3->name );
  $$->name = (char *) malloc( 20 );
  sprintf( $$->name, "[ebp-%d]", 4*(paramcount+1) );
  paramcount++;
  sprintf( $$->code, "%s/tmov %s, eax/n", $$->code, $$->name );
  break;
  case '-':
  $$->code = (char *) malloc( strlen($1->code) + strlen($1->name) + strlen($3->name) + 1024 );
  sprintf( $$->code, "%s%s/tmov eax, %s/n/tsub eax, %s/n", $3->code, $1->code, $1->name, $3->name );
  $$->name = (char *) malloc( 20 );
  sprintf( $$->name, "[ebp-%d]", 4*(paramcount+1) );
  paramcount++;
  sprintf( $$->code, "%s/tmov %s, eax/n", $$->code, $$->name );
  break;
  break;
  case OR:
  break;
  }
  $$->value = 0;
  }
  | term
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + 1 );
  if( $1->is_const == 1 )
  {
  strcpy( $$->code, "");
  }
  else
  {
  strcpy( $$->code, $1->code );
  }
  $$->name = $1->name;
  $$->value = 1;
  }
  ;
  addop : '+'
  {
  $$ = (char *) malloc( sizeof( char ) );
  *$$ = '+';
  }
  | '-'
  {
  $$ = (char *) malloc( sizeof( char ) );
  *$$ = '-';
  }
  ;
  term : term mulop unary_expression
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  switch( *$2 )
  {
  case '*':
  $$->code = (char *) malloc( strlen($1->code) + strlen($1->name) + strlen($3->name) + 1024 );
  sprintf( $$->code, "%s/tmov eax, %s/n/tmov ebx, %s/n/timul ebx/n", $1->code, $1->name, $3->name );
  $$->name = (char *) malloc( 20 );
  sprintf( $$->name, "[ebp-%d]", 4*(paramcount+1) );
  paramcount++;
  sprintf( $$->code, "%s/tmov %s, eax/n", $$->code, $$->name );
  break;
  case '/':
  $$->code = (char *) malloc( strlen($1->code) + strlen($1->name) + strlen($3->name) + 1024 );
  sprintf( $$->code, "%s/tmov eax, %s/n/tcdq/n/tmov ebx, %s/n/tidiv ebx/n", $1->code, $1->name, $3->name );
  $$->name = (char *) malloc( 20 );
  sprintf( $$->name, "[ebp-%d]", 4*(paramcount+1) );
  paramcount++;
  sprintf( $$->code, "%s/tmov %s, eax/n", $$->code, $$->name );
  break;
  case '%':
  $$->code = (char *) malloc( strlen($1->code) + strlen($1->name) + strlen($3->name) + 1024 );
  sprintf( $$->code, "%s/tmov eax, %s/n/tcdq/n/tmov ebx, %s/n/tidiv ebx/n", $1->code, $1->name, $3->name );
  $$->name = (char *) malloc( 20 );
  sprintf( $$->name, "[ebp-%d]", 4*(paramcount+1) );
  paramcount++;
  sprintf( $$->code, "%s/tmov %s, edx/n", $$->code, $$->name );
  break;
  }
  $$->is_const = 0;
  }
  | unary_expression
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + 1 );
  if( $1->is_const == 1 )
  {
  strcpy( $$->code, "");
  }
  else
  {
  strcpy( $$->code, $1->code );
  }
  $$->name = $1->name;
  $$->is_const = $1->is_const;
  }
  ;
  mulop : '*'
  {
  $$ = (char *) malloc( sizeof( char ) );
  *$$ = '*';
  }
  | '/'
  {
  $$ = (char *) malloc( sizeof( char ) );
  *$$ = '/';
  }
  | '%'
  {
  $$ = (char *) malloc( sizeof( char ) );
  *$$ = '%';
  }
  ;
  unary_expression : unaryop unary_expression
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->code ) + 10 );
  sprintf( $$->code, "%s/tmov ebx, %s/n/tneg ebx/n", $2->code, $2->name );
  $$->name = (char *) malloc( 6 );
  sprintf( $$->name, "ebx" );
  $$->is_const = 0;
  }
  | factor
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = $1->code;
  $$->name = $1->name;
  $$->is_const = $1->is_const;
  }
  ;
  unaryop : '-'
  {
  $$ = (char *) malloc( sizeof( char ) );
  *$$ = NEG;//用数NEG表示负号;
  }
  ;
  factor : '(' expression ')'
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $2->code ) + 1 );
  sprintf( $$->code, "%s", $2->code );
  $$->name = $2->name;
  $$->is_const = 0;
  }
  | var
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  if( $1->value == 0 )
  {
  $$->code = (char *) malloc( strlen( $1->code ) + 1 );
  sprintf( $$->code, "%s", $1->code );
  $$->name = $1->name;
  $$->is_const = 1;
  }
  else if( $1->value == 1 )
  {
  $$->code = (char *) malloc( strlen( $1->name ) + 1 );
  sprintf( $$->code, "%s", $1->name );
  $$->name = $1->code;
  $$->is_const = 1;
  }
  }
  | call
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->code ) + 1 );
  sprintf( $$->code, $1->code );
  $$->name = ( char * )malloc(4);
  sprintf( $$->name, "eax" );
  $$->code = $1->code;
  $$->is_const = 0;
  }
  | NUM
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->name ) + 1 );
  sprintf( $$->code, "");
  $$->is_const = 1;
  $$->value = atoi( $1->name );
  $$->name = $1->name;
  }
  | STRING
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->name ) + 1 );
  sprintf( $$->code, "");
  $$->is_const = 1;
  $$->name = (char *) malloc( 20 );
  sprintf( $$->name, "_msg%d", msgIndex );
  sprintf( varBuffer, "%s/t%s BYTE %s, 0/n", varBuffer, $$->name, $1->name );
  sprintf( $$->name, "offset _msg%d", msgIndex );
  msgIndex++;
  }
  ;
  call : ID '(' args ')'
  {
  if( lookup( $1->name, currentTable ) == NULL )
  {
  char msg[100];
  sprintf( msg, "The Function %s must be declared: ", $1->name );
  yyerror( msg );
  }
  
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->name ) + strlen( $3->code) + 50 );
  sprintf( $$->code, "%s/tcall %s/n", $3->code, $1->name );
  }
  ;
  args : /*空值*/
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( 1 );
  sprintf( $$->code, "");
  }
  | arg_list
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen($1->code) + strlen( $1->temp ) + 1 );
  sprintf( $$->code, "%s%s", $1->temp, $1->code );
  }
  ;
  arg_list : arg_list ',' expression
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen($1->code) + strlen($3->code) + 10 );
  sprintf( $$->code, "/tpush %s/n%s", $3->name, $1->code );
  $$->temp = (char *) malloc( strlen( $1->temp ) + strlen( $3->code ) + 1 );
  sprintf( $$->temp, "%s%s", $1->temp, $3->code );
  callparam++;
  }
  | expression
  {
  callparam = 1;
  $$ = (expr *) malloc( sizeof( expr ) );
  $$->code = (char *) malloc( strlen( $1->name ) + 10 );
  sprintf( $$->code, "/tpush %s/n", $1->name );
  $$->temp = (char *) malloc( strlen( $1->code ) + 10 );
  sprintf( $$->temp, "%s", $1->code );
  }
  ;
  calling_convention : __CDECL
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  *$$ = 1;
  }
  | __STDCALL
  {
  $$ = (expr *) malloc( sizeof( expr ) );
  *$$ = 0;
  }
  ;
  %%
  #include "Lex.yy.c"
  int yyparse();
  void yyerror();
  int main( int argc, char **argv )
  {
  FILE *fp ;
  currentTable = table( NULL, 0 );
  currentLevel = 0;
  ++argv;
  --argc;
  if(argc >0)
  yyin = fopen(argv[0],"r");
  else
  yyin = stdin;
  ++argv;
  --argc;
  if(argc >0)
  yyout = fopen(argv[0],"w");
  else
  yyout = stdout;
  // fp = fopen( "a.txt", "r" );
  // yyin = fp;
  // fp = fopen( "c.asm", "w" );
  // yyout = fp;
  return(yyparse());
  }
  void yyerror( char *temp )
  {
  printf( "/nline %d: %s %s %d/n", lineno, temp, yytext, currentLevel );
  exit(1);
  }
  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=402314

本文转自
http://blog.csdn.net/adminxp/archive/2005/06/24/402314.aspx  
【信息科学与工程学】【数据中心】第三十五篇 云计算数据中心的学科知识04 编号学科(课程)核心知识点在云计算/云存储/云网络/云安全/云MaaS中的作用代表教材/资料/论文 + 数学方程式列表工业界应用D1421​云原生数据库:TiDB​HTAP(混合事务/分析处理)、分布式SQL、水平扩展、强一致(Raft)、自动故障恢复、与MySQL兼容、TiFlash列式引擎提供弹性扩展的分布式数据库;支撑高并发在线交易与实时分析教材:《TiDB in Action》PingCAP(2020) 论文:《TiDB: A Raft-based HTAP Database》(2017) 阅读详情

相关推荐

《编译原理》课程设计报告(简略版)

本次课程设计旨在利用离散数学和形式语言的基本知识,设计和实现一个简单语言的编译器。设计内容包括上下文无关文法、属性文法的构造,以及编译器前端(词法分析、语法分析、中间代码生成)和后端(优化、目标代码生成)的实现。通过LL(1)预测分析方法进行语法分析,并生成四元式中间代码,最终转化为汇编代码。设计过程中需选择合适的开发工具和测试工具,验证编译器功能并评估其局限性。此外,课程设计还强调了对新型编译技术和国产编译器的了解,旨在培养学生的科技创新能力和家国情怀。

执一抹浅笑的博客 618

基于C语言的编译器设计与实现

目录 1选题背景 1 2系统关键定义 2 2.1单词文法描述 2 2.2语句文法描述 3 2.3 符号表结构描述 4 2.4 错误类型码描述 5 3系统设计与实现 6 3.1编译程序符号表结构 6 3.2编译程序报错功能 7 3.3词法语法分析器(实验一) 7 3.4语义分析(实验二) 8 4系统测试与评价 10 4.1 测试用例 10 4.2 正确性测试 11 4.3 报错功能测试 15 4.4 系统的优点 20 4.5 系统的缺点 20 5实验小结或体会 21 参考文献 22 附件:源代码 23 附录一

毕业作品网站 2160

探索Tiny C Compiler实现与编译原理课程设计

本文还有配套的精品资源,点击获取 简介:本课程设计项目以Tiny C Compiler(TCC)为核心,深入研究编译器的工作原理。TCC是一个轻量级、高效的C语言编译器,通过其实现可以直观地理解编译过程中的词法分析、语法分析、语义分析及代码生成等核心步骤。学生将通过编写词法分析器、解析器和实现语义分析等环节,亲身体验并掌握编译原理,加深对计算机科学这一重要分支的理解。 ...

weixin_35916518的博客 1199

【编译原理】编译器设计文档

20级软院编译原理——编译器设计文档

intpyjl的博客 1342

编译原理课程设计

(2)扩充函数:① 有返回值和返回语句;(3) 实现2种注释: // 和/* */(4) 增加类型:字符类型、实数类型。(1)增加类型:字符类型、实数类型。(3) 实现2种注释: // 和/(4)其他难度相当的典型语言设施。(2) 扩充前后++和前后–运算。(2) 扩充前后++和前后–运算。(5) 增加一维数组类型。(3)增加一维数组类型。

weixin_54225829的博客 485

编译原理-简单编译器课程设计(C语言源代码)(附实验报告)

完成一个相对完整的编译器,该编译完成对变量类型(整数/浮点数)定义、赋值、四则运算、逻辑运算、跳转与循环控制功能。其输入是源程序(参见&ldquo;一个四则运算源程序示例&rdquo;),输出是所有变量的最终值。

编译原理实验课程设计

编译原理实验课程设计 -词法语法分析综合设计 1 概述 通过C++实现词法语法综合分析,可以通过用户输入判断用户所需要实现的功能,做出相对应的操作。 2 实验目标 理解并掌握词法,语法分析的原理与方法。 能够使用某种语言实现词法,语法分析程序。 对编译的基本概念,原理和方法有完整和清楚的理解,并能正确而熟练的运用。 3 实验算法描述 3.1 实验要求 在程序开始位置输入对应功能字母编码,通过键盘字母选择,进行词法分析、语法分析、综合分析 在综合设计中,采用词法分析程序作为语法分析程序子程序的方法,实现词法

c1719561053的博客 1万+

编译原理课程设计 -----C语言编译器

包含大部分C语言语法规则 包括赋值声明语句 ,if语句, for语句 ,switch语句,while语句 词法,语法,语义分析都有 支持相互嵌套,支持拉链回填~

【编译原理】【C语言】实验三:递归下降分析法

递归下降分析法 1、实验内容 2、前期准备 2.1 递归下降分析法原理 2.2 要实现的文法 2.3 需要的函数 3、分析过程 3.1 递归下降分析法设计思想及算法 3.2 分析栈的分析过程 3.3 流程图 3.4 源代码 3.5 运行结果 4、遇到问题 1、实验内容   用高级语言实现递归下降分析程序。使用输入串i*(i+i),输出分析栈中所有内容,并给出分析结果。 2、前期准备 2.1 递归下降分析法原理   自顶向下分析就是从文法的开始符触发并寻找出这样一个推导序列:推导出的句子恰好为输入符号串;或

zoxiii的博客 4万+

编译原理课程设计之编译器(完整代码 + 测试样例)

编译原理课程设计之编译器(完整代码 + 测试样例):包含了完整的编译器源代码和测试样例,内容上实现了一体化的词法分析 + 语法分析 + 语法制导翻译,最后生成对应汇编指令

编译原理课程设计C-语言编译器

绝对能帮你完成一个像样的C-语言编译器和一个课程设计,尤其四川大学的同学,真的是福利。

编译原理课程设计——小型编译器的设计与实现

设计内容 实现一个小型编译程序。 (1) 输入:高级语言源程序; (2) 输出:四元式程序、汇编语言程序。

qq_50699078的博客 4117

编译原理课程设计&mdash;&mdash;简单编译器

编译原理课程设计,简单编译器 希望对大家有帮助,不过话说是学校的作业,仅供参考的说

编译原理课程设计 基于Java的LL(1)文法预测分析程序

1概述语法分析器的任务在于,对词法分析器识别出的单词符号串进行识别分析,如程序中的表达式、说明语句等各类型语句,对于语法分析主要采取的是上下文无关文法进行描述的,且主要分为自上而下的语法分析以及自下而上的语法分析,而在自上而下的语法分析中存在回溯现象,为了避免回溯,在自上而下的语法分析方法中,主要采用LL(1)语法分析方法,LL(1)分析方法的主要特点为:文法不含左递归,文法中每一个非终结符的各个...

qq959261982的博客 3578

编译原理课程设计 java实现编译器

java实现c语言编译器,包括词法分析,语法分析,语义分析,目标代码生成四个部分,总体实现编译器功能,有运行界面,代码精简,值得下载。

Pcileech(DMA)开源项目简述

希望大DMA家能正确认识FPGA,正确使用DMA技术,对于市场上各类伪装成花里胡哨设备的FPGA板卡它的本质是不会变的,不要上当受骗。

2301_79773847的博客 5968

【编译原理 课程设计】设计SAMPLE语言的语法分析器 github源码(C++、LL(1)分析、语法制导翻译和中间代码生成)

这里是lz方法大二上的编译原理实验课,课设要求模仿编译器的原理思想,用编译语言设计能够识别单词和简单语法的编译,当时摸了两天左右写了小一千行,在这里再回顾一下思路,并留下对应的源码供参考。 实验实现的方法有很多种,其中实验一的词法分析部分比较简单,这里重点分析语法分析部分。 题目需求: 实验一:词法分析 这里用了stl::map等提前存储单词记录出现次数即可。这里就不展开详细描述了。一些具体细节的可参考github中的源码。 map&lt;string, int...

m0_51776409的博客 2722

【温室气体数据集】CAMS 发布的温室气体(GHG)数据集总结

【温室气体数据集】CAMS 发布的温室气体(GHG)数据集总结

WW、forever的博客 561

编译原理课设

一、课程设计内容 1.1、前置条件:有效文法及正确的算符优先表 1.2、功能需求:根据文法及算符优先表,实现算符优先分析过程,输入串采用教材133页习题2中(2)中的字符串(#(((a,a),x,(a)),a)#)。 1.3、开发环境: Java(jdk1.8) Windows 10 二、数据结构设计 2.1、类 2.1.1、文法对象类 p...

a597097186的博客 5586

手把手教你学Simulink--基于新能源并网与协调控制的场景实例:双馈异步风力发电机并网运行仿真

随着“双碳”目标的推进,风力发电已成为新能源并网的核心组成部分。其中,双馈异步风力发电机(Doubly-Fed Induction Generator, DFIG)因其变频器容量小(仅处理转差功率)、可实现有功/无功独立调节、运行效率高等优势,广泛应用于现代兆瓦级风电机组。DFIG系统通过转子侧变流器(Rotor-Side Converter, RSC)和网侧变流器(Grid-Side Converter, GSC)实现对发电机转矩与电网无功的精确控制,支持最大功率点跟踪(MPPT)和。

MHD0815的博客 288

自适应控制 (Adaptive Control) 算法详解及案例分析

自适应控制是一种能够在线调整控制器参数以适应系统动态变化的控制方法。其核心思想是通过实时估计系统参数或调整控制器参数,使系统在不确定性或动态变化的环境中仍能保持稳定性和性能。

闲人编程的博客 4090
上一篇: Javac1.7编译器源码分析_简介与安装
下一篇: 101部世界名著浓缩成一句话(2.0版)
chief1985
博客等级 码龄22年 1546粉丝 879原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值