Skip to content

Instantly share code, notes, and snippets.

@VedantParanjape
Created May 18, 2020 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VedantParanjape/3f4d80ca681830b9a8de9be4fd5472ad to your computer and use it in GitHub Desktop.
Save VedantParanjape/3f4d80ca681830b9a8de9be4fd5472ad to your computer and use it in GitHub Desktop.
%{
#include <stdio.h>
#include <iostream>
using namespace std;
int linenumbers = 0;
extern int yylex();
%}
%option noyywrap
%x comment
%%
[ \t] ;
[@][0-9]+ { printf("found a constant: %s\n", yytext); return 1; }
^[@][a-zA-Z]+.* { printf("found a variable: %s\n", yytext); return 1; }
^[.*]{1,3}[=]* { printf("found jump statement")}
\n { linenumbers++; return 1; }
. ;
"/*" { BEGIN(comment); }
<comment>"*/" { BEGIN(INITIAL); }
<comment>\n { linenumbers++; }
<comment>. ;
%%
int main(int, char**) {
// Open a file handle to a particular file:
FILE *myfile = fopen("../examples/add/Add.asm", "r");
// Make sure it is valid:
if (!myfile) {
cout << "I can't open a.snazzle.file!" << endl;
return -1;
}
// Set Flex to read from it instead of defaulting to STDIN:
yyin = myfile;
// Parse through the input:
while(yylex());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment