Files
compiler-the-translators/lexicalStructure.lex
2025-02-12 12:41:23 -05:00

51 lines
1.5 KiB
Plaintext

/* so we placed the citation here. */
/* definitions */
%option noyywrap
%{
#include "typedefs.h"
%}
DIGIT [0-9]
CHAR \\n|\\t|\\'|[^'\n\t\\]
/* char can be a newline, tab, an escaped quote, or anything but a single quote, an actual line break, an actual tab, or a backslash by itself (to prevent confusion from escaped quote */
SCHAR \\n|\\t|\\\"|[^\"\n\\]
/*similar to above, a string Char (SCHAR) is the same as a CHAR except we cannot have double quotes instead of single quotes. Double quotes need to be escaped in Flex unlike single quotes based on documentation */
STARTCOM \(\*
ENDCOM \*\)
COMMENTCHAR [^\*]|\*[^\)]
/*Making the contents of a comment anything that is either not a * or not a * followed by ) to terminate comments at the first ENDCOM */
%%
/* rules */
{DIGIT}+ {printf( "C_INTEGER: %s (%d)\n", yytext, atoi( yytext ) );}
"null" {printf( "C_NULL: %s (%d)\n", yytext, atoi( yytext ) );}
'{CHAR}' {printf( "C_CHARACTER: %s (%d)\n", yytext, atoi( yytext ) );} /*using double \ per documentation to show escaped chars*/
"true" {printf( "C_TRUE: %s (%d)\n", yytext, atoi( yytext ) );}
"false" {printf( "C_FALSE: %s (%d)\n", yytext, atoi( yytext ) );}
\"{SCHAR}*\" {printf( "C_STRING: %s (%d)\n", yytext, atoi( yytext ) );}
\(\*{COMMENTCHAR}*\*\) {printf( "COMMENT: %s (%d)\n", yytext, atoi( yytext ) );}
.|\n
%%
/* user code */
int main( int argc, char **argv )
{
argc--, argv++; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
}