summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkugwa <kugwa2000@gmail.com>2015-10-21 13:53:57 +0800
committerkugwa <kugwa2000@gmail.com>2015-10-21 13:53:57 +0800
commitcfc90007070c42c1b205b1d77b3ab3b61b0fba4c (patch)
tree413bee9a060273378451519a9b0ba8faa6ff7f78
parent4aec326d0b910584f728e55c223738c033c43744 (diff)
downloadcompiler2015-cfc90007070c42c1b205b1d77b3ab3b61b0fba4c.tar
compiler2015-cfc90007070c42c1b205b1d77b3ab3b61b0fba4c.tar.gz
compiler2015-cfc90007070c42c1b205b1d77b3ab3b61b0fba4c.tar.bz2
compiler2015-cfc90007070c42c1b205b1d77b3ab3b61b0fba4c.tar.lz
compiler2015-cfc90007070c42c1b205b1d77b3ab3b61b0fba4c.tar.xz
compiler2015-cfc90007070c42c1b205b1d77b3ab3b61b0fba4c.tar.zst
compiler2015-cfc90007070c42c1b205b1d77b3ab3b61b0fba4c.zip
Deal with identifiers and reserved words differently
-rw-r--r--lexer.l34
1 files changed, 19 insertions, 15 deletions
diff --git a/lexer.l b/lexer.l
index e2bb8f2..54cd62a 100644
--- a/lexer.l
+++ b/lexer.l
@@ -1,8 +1,11 @@
%option noyywrap
%{
#include <stdio.h>
+#include <string.h>
#include "symbol-table.h"
+#define SIZE_OF_ARR(x) (sizeof(x)/sizeof(x[0]))
+
int linenumber;
/* You need to define for all tokens in C--, here are some examples */
@@ -87,20 +90,25 @@ ERROR .
{WS} {}
{ID} {
- symtab * ptr;
- ptr = lookup(yytext);
- if (ptr == NULL)
- insertID(yytext);
- else
- ptr->counter++;
+ int i;
+ char *reserved[] = {"return", "typedef", "if", "else", "int", "float", "for", "void", "while"};
+
+ for (i = 0; i < SIZE_OF_ARR(reserved); i++)
+ if (strcmp(yytext, reserved[i]) == 0) break;
+ if (i == SIZE_OF_ARR(reserved)) {
+ symtab * ptr;
+ ptr = lookup(yytext);
+ if (ptr == NULL)
+ insertID(yytext);
+ else
+ ptr->counter++;
+ }
}
{CONST_INT} {}
{CONST_FLOAT} {}
{CONST_STRING} {}
-{COMMENT} {
- puts(yytext);
- }
+{COMMENT} puts(yytext);
{OP_ASSIGN} {}
{OP_OR} {}
{OP_AND} {}
@@ -116,9 +124,7 @@ ERROR .
{OP_NE} {}
{OP_EQ} {}
-{NEWLINE} {
- linenumber++;
- }
+{NEWLINE} linenumber++;
{DL_LPAREN} {}
{DL_RPAREN} {}
@@ -130,9 +136,7 @@ ERROR .
{DL_SEMICOL} {}
{DL_DOT} {}
-{ERROR} {
- fputs("ERR", stderr);/* return ERROR; */
- }
+{ERROR} fputs("ERR", stderr);/* return ERROR; */
%%