| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- D [0-9]
- L [a-zA-Z_]
- H [a-fA-F0-9]
- E [Ee][+-]?{D}+
- FS (f|F|l|L)
- IS (u|U|l|L)*
- %{
- #pragma GCC diagnostic ignored "-Wall"
- #pragma GCC diagnostic ignored "-Wextra"
- #pragma GCC diagnostic ignored "-Wconversion"
- #pragma GCC diagnostic ignored "-Wpedantic"
- #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
- #include <stdio.h>
- #include "C.tab.h"
- #include <string.h>
- #include <stdlib.h>
- #include "token.h"
- TOKEN* make_string(char*);
- extern TOKEN* lookup_token(char*);
- TOKEN* make_int(char*);
- TOKEN* lasttok;
- void count(void);
- void comment(void);
- %}
- %%
- "/*" { comment(); }
- "auto" { count(); return(AUTO); }
- "break" { count(); return(BREAK); }
- "continue" { count(); return(CONTINUE); }
- "else" { count(); return(ELSE); }
- "extern" { count(); return(EXTERN); }
- "if" { count(); return(IF); }
- "int" { count(); return(INT); }
- "function" { count(); return(FUNCTION); }
- "return" { count(); return(RETURN); }
- "void" { count(); return(VOID); }
- "while" { count(); return(WHILE); }
- {L}({L}|{D})* { count(); lasttok = lookup_token(yytext);
- return(IDENTIFIER); }
- {D}+{IS}? { count(); lasttok = make_int(yytext);return(CONSTANT); }
- L?'(\\.|[^\\'])+' { count(); lasttok = make_int(yytext);return(CONSTANT); }
- L?\"(\\.|[^\\"])*\" { count(); lasttok = make_string(yytext);
- return(STRING_LITERAL); }
- "<=" { count(); return(LE_OP); }
- ">=" { count(); return(GE_OP); }
- "==" { count(); return(EQ_OP); }
- "!=" { count(); return(NE_OP); }
- ";" { count(); return(';'); }
- "{" { count(); return('{'); }
- "}" { count(); return('}'); }
- "," { count(); return(','); }
- ":" { count(); return(':'); }
- "=" { count(); return('='); }
- "(" { count(); return('('); }
- ")" { count(); return(')'); }
- "!" { count(); return('!'); }
- "-" { count(); return('-'); }
- "+" { count(); return('+'); }
- "*" { count(); return('*'); }
- "/" { count(); return('/'); }
- "%" { count(); return('%'); }
- "<" { count(); return('<'); }
- ">" { count(); return('>'); }
- [ \t\v\n\f] { count(); }
- . { /* ignore bad characters */ }
- %%
- int yywrap(void)
- {
- return(1);
- }
- void comment(void)
- {
- char c, c1;
- loop:
- while ((c = input()) != '*' && c != 0)
- putchar(c);
- if ((c1 = input()) != '/' && c != 0)
- {
- unput(c1);
- goto loop;
- }
- if (c != 0)
- putchar(c1);
- }
- int column = 0;
- void count()
- {
- int i;
- for (i = 0; yytext[i] != '\0'; i++)
- if (yytext[i] == '\n')
- column = 0;
- else if (yytext[i] == '\t')
- column += 8 - (column % 8);
- else
- column++;
- ECHO;
- }
- TOKEN *new_token(int type)
- {
- TOKEN *ans = (TOKEN*)malloc(sizeof(TOKEN));
- ans->type = type;
- return ans;
- }
- TOKEN *make_string(char *s)
- {
- TOKEN *ans = new_token(STRING_LITERAL);
- int len = strlen(s);
- ans->lexeme = (char*)calloc(1, len-1);
- strncpy(ans->lexeme, s+1, len-2);
- return ans;
- }
- TOKEN *make_int(char *s)
- {
- int n = *s!='\'' ? atoi(s) : *(s+1);
- TOKEN *ans = new_token(CONSTANT);
- ans->value = n;
- asprintf(&ans->lexeme, "%d", n);
- return ans;
- }
|