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

📄 ccc_1.l

📁 c语言的简化编译器
💻 L
字号:
/* subset of c, lex grammer.  */
/* version 0.01               */
/* chenhongyu. 2004-9-16      */

%{
#include "lang.tab.h"
#include <string.h>
#include "memory.h"
#include "error.h"

extern int lineno;

int commentDepth=0;
%}

%x commentsc 

%%
<commentsc>{
  \n                       lineno++;
  "*/"                   { commentDepth--; if (commentDepth == 0) BEGIN(INITIAL);}
  "/*"                     commentDepth++;
  "//"[^\n]*             /* Single line comments take precedence over multilines */;
  <<EOF>>                  yyerror("Unclosed comment.");
  [^*/\n]*               /* ignore anything thats not a '*' or '/' to increase speed */;
  .                      /* ignore (multiline comments)*/;
}

[ \t]+                /* ignore */;

"/*"                   { commentDepth++; BEGIN(commentsc);}

\n                       lineno++;
"//"[^\n]*            /* ignore (singleline comment)*/;

bool                     return tBOOL;
else                     return tELSE;
for                      return tFOR;
if                       return tIF;
int                      return tINT;
return                   return tRETURN;
string                   return tSTRING;
void                     return tVOID;
while                    return tWHILE;

"&&"                     return tAND;
"=="                     return tEQUALS;
">="                     return tGEQUALS;
"<="                     return tLEQUALS;
"!="                     return tNEQUALS;
"||"                     return tOR;
"="                      return '=';
">"                      return '>';
"<"                      return '<';
"!"                      return '!';
"+"                      return '+';
"-"                      return '-';
"*"                      return '*';
"/"                      return '/';
"%"                      return '%';
"{"                      return '{';
"}"                      return '}';
";"                      return ';';
"("                      return '(';
")"                      return ')';
","                      return ',';

0|([1-9][0-9]*)        { yylval.intconst = atoi(yytext);
                         return tINTCONST; }
true                   { yylval.boolconst = 1;
                         return tBOOLCONST; }
false                  { yylval.boolconst = 0;
                         return tBOOLCONST; }
\"[^\"\n]*\"           { yytext[strlen(yytext)-1]='\0'; /* remove "'s from yytext */
                         yylval.stringconst = strdup(yytext+1);
                          return tSTRINGCONST; }
[A-Za-z_][A-Za-z0-9_]* { yylval.identifier = strdup(yytext);
                         return tIDENTIFIER; }

.                        yyerror("Invalid character '%s'.", yytext);

%%

⌨️ 快捷键说明

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