tiny.l

来自「一个简化的编译器」· L 代码 · 共 76 行

L
76
字号
/****************************************************//* File: tiny.l                                     *//* Lex specification for TINY                       *//* Compiler Construction: Principles and Practice   *//* Kenneth C. Louden                                *//****************************************************/%{#include "globals.h"#include "util.h"#include "scan.h"/* lexeme of identifier or reserved word */char tokenString[MAXTOKENLEN+1];%}digit       [0-9]number      {digit}+letter      [a-zA-Z]identifier  {letter}+newline     \nwhitespace  [ \t]+%%"if"            {return IF;}"then"          {return THEN;}"else"          {return ELSE;}"end"           {return END;}"repeat"        {return REPEAT;}"until"         {return UNTIL;}"read"          {return READ;}"write"         {return WRITE;}":="            {return ASSIGN;}"="             {return EQ;}"<"             {return LT;}"+"             {return PLUS;}"-"             {return MINUS;}"*"             {return TIMES;}"/"             {return OVER;}"("             {return LPAREN;}")"             {return RPAREN;}";"             {return SEMI;}{number}        {return NUM;}{identifier}    {return ID;}{newline}       {lineno++;}{whitespace}    {/* skip whitespace */}"{"             { char c;                  do                  { c = input();                    if (c == EOF) break;                    if (c == '\n') lineno++;                  } while (c != '}');                }.               {return ERROR;}%%TokenType getToken(void){ static int firstTime = TRUE;  TokenType currentToken;  if (firstTime)  { firstTime = FALSE;    lineno++;    yyin = source;    yyout = listing;  }  currentToken = yylex();  strncpy(tokenString,yytext,MAXTOKENLEN);  if (TraceScan) {    fprintf(listing,"\t%d: ",lineno);    printToken(currentToken,tokenString);  }  return currentToken;}

⌨️ 快捷键说明

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