lexer.l

来自「学习lemon语法分析的windows程序」· L 代码 · 共 64 行

L
64
字号
%{#include "lexglobal.h" #include "example5.h"#include <string.h>#include <math.h>int line = 1, col = 1;%}%%[0-9]+|[0-9]*\.[0-9]+    {                      col += (int) strlen(yytext);                                                 yylval.dval = atof(yytext);                                                 return NUM; }[ \t]   { col += (int) strlen(yytext); }               /* ignore but count white space */[A-Za-z][A-Za-z0-9]*                           { /* ignore but needed for variables */                                                return 0;                                               }"+"           {  return PLUS; }"-"           {  return MINUS; }"*"           {  return TIMES; }"/"           {  return DIVIDE; }\n      { col = 0; ++line; return NEWLINE; }.       { col += (int) strlen(yytext); return yytext[0]; }%%/** * reset the line and column count * * */void reset_lexer(void){  line = 1;  col  = 1;}/** * yyerror() is invoked when the lexer or the parser encounter * an error. The error message is passed via *s * * */void yyerror(char *s){  printf("error: %s at line: %d col: %d\n",s,line,col);}int yywrap(void){  return 1;}

⌨️ 快捷键说明

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