Permalink
Browse files

Generate tokens

  • Loading branch information...
halogenandtoast committed Dec 30, 2014
1 parent f5c10a1 commit 6079c33f7965dd4465a84c372dc932a93a858ade
Showing with 43 additions and 35 deletions.
  1. +5 −3 parse.y
  2. +38 −32 ruby.l
@@ -1,13 +1,15 @@
%{
#include <stdio.h>
extern int yylex(void);
void yyerror(char const *s) { fprintf(stderr, "%s\n", s); }
extern void yyerror(char const *s);
%}
%left tPLUS
%token tNUMBER
%token tPLUS
%token tSTRING tFLOAT tNUMBER tID tCONSTANT tEQUAL tGT tLT tGTE tLTE tNEQUAL
%token tPLUS tMINUS tMULT tDIV tMOD tEMARK tQMARK tAND tOR tLSBRACE tRSBRACE
%token tLPAREN tRPAREN tLBRACE tRBRACE tAT tDOT tCOMMA tCOLON
%start program
%%
70 ruby.l
@@ -2,46 +2,52 @@
#include <stdio.h>
#include "parse.tab.h"
#define VTYPE(type, value) printf("%s(%s)\n", type, value)
#define TYPE(type) printf("%s\n", type)
#define TOKEN(id) return t##id
%}
%option noyywrap
%option yylineno
NUMBER [0-9](_[0-9]|[0-9])*
%%
#.*$ {}
\"([^"]|\\.)*\" { VTYPE("STRING", yytext); }
\'([^']|\\.)*\' { VTYPE("STRING", yytext); }
{NUMBER}(\.{NUMBER}|(\.{NUMBER})?[eE][+-]?{NUMBER}) { VTYPE("FLOAT", yytext); }
{NUMBER} { yylval = atoi(yytext); return tNUMBER; }
[a-z_][a-zA-Z0-9_]* { VTYPE("ID", yytext); }
[A-Z][a-zA-Z0-9_]* { VTYPE("CONSTANT", yytext); }
"=" { TYPE("EQUAL"); }
">" { TYPE("GT"); }
"<" { TYPE("LT"); }
">=" { TYPE("GTE"); }
"<=" { TYPE("LTE"); }
"!=" { TYPE("NEQUAL"); }
"+" { return tPLUS; }
"-" { TYPE("MINUS"); }
"*" { TYPE("MULT"); }
"/" { TYPE("DIV"); }
"%" { TYPE("MOD"); }
"!" { TYPE("EMARK"); }
"?" { TYPE("QMARK"); }
"&" { TYPE("AND"); }
"|" { TYPE("OR"); }
"[" { TYPE("LSBRACE"); }
"]" { TYPE("RSBRACE"); }
"(" { TYPE("LPAREN"); }
")" { TYPE("RPAREN"); }
"{" { TYPE("LBRACE"); }
"}" { TYPE("RBRACE"); }
"@" { TYPE("AT"); }
"." { TYPE("DOT"); }
"," { TYPE("COMMA"); }
":" { TYPE("COLON"); }
\"([^"]|\\.)*\" { VTYPE("STRING", yytext); TOKEN(STRING); }
\'([^']|\\.)*\' { VTYPE("STRING", yytext); TOKEN(STRING); }
{NUMBER}(\.{NUMBER}|(\.{NUMBER})?[eE][+-]?{NUMBER}) { VTYPE("FLOAT", yytext); TOKEN(FLOAT); }
{NUMBER} { yylval = atoi(yytext); TOKEN(NUMBER); }
[a-z_][a-zA-Z0-9_]* { VTYPE("ID", yytext); TOKEN(ID); }
[A-Z][a-zA-Z0-9_]* { VTYPE("CONSTANT", yytext); TOKEN(CONSTANT); }
"=" { TOKEN(EQUAL); }
">" { TOKEN(GT); }
"<" { TOKEN(LT); }
">=" { TOKEN(GTE); }
"<=" { TOKEN(LTE); }
"!=" { TOKEN(NEQUAL); }
"+" { TOKEN(PLUS); }
"-" { TOKEN(MINUS); }
"*" { TOKEN(MULT); }
"/" { TOKEN(DIV); }
"%" { TOKEN(MOD); }
"!" { TOKEN(EMARK); }
"?" { TOKEN(QMARK); }
"&" { TOKEN(AND); }
"|" { TOKEN(OR); }
"[" { TOKEN(LSBRACE); }
"]" { TOKEN(RSBRACE); }
"(" { TOKEN(LPAREN); }
")" { TOKEN(RPAREN); }
"{" { TOKEN(LBRACE); }
"}" { TOKEN(RBRACE); }
"@" { TOKEN(AT); }
"." { TOKEN(DOT); }
"," { TOKEN(COMMA); }
":" { TOKEN(COLON); }
[\t ] {}
\n {}
. { fprintf(stderr, "Unknown token '%s'\n", yytext); }
%%
void yyerror(char const *s) {
fprintf(stderr, "%s. Unexpected \"%s\" on line %d\n", s, yytext, yylineno);
}

0 comments on commit 6079c33

Please sign in to comment.