This commit is contained in:
maxsimongt3
2025-02-11 16:55:01 -05:00
committed by GitHub
5 changed files with 137 additions and 18 deletions

View File

@ -1,14 +1,16 @@
# Basic Makefile example from flex documentation -- provides explicit rules
# Creates "myprogram" from "scan.l" and "myprogram.c"
#
#LEX=flex
#myprogram: scan.o myprogram.o
#$(CC) -o $@ $(LDFLAGS) $^
#myprogram.o: myprogram.c
#$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^
#scan.o: scan.c
#$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $^
#scan.c: scan.l
#$(LEX) $(LFLAGS) -o $@ $^
#clean:
#$(RM) *.o scan.c
CC := gcc
FLEX := flex
LEX := lexicalStructure.lex
EXE := lexicalStructure
lexicalStructure:
$(FLEX) $(LEX)
$(CC) lex.yy.c -o $(EXE)
test_operators:
./$(EXE) ./tests/test_operators.alpha
clean:
rm -f *.o
rm -f lex.yy.c
rm -f $(EXE)

View File

@ -1,14 +1,26 @@
/* Lexical Analysis with Flex (2.6.0) We used some of the code from this manual */
/* Lexical Analysis with Flex (1.6.0) We used some of the code from this manual */
/* so we placed the citation here. */
/* definitions */
%option noyywrap
%{
#include "typedefs.h"
%{
#include "typedefs.h"
%}
int line_number = 1, column_number = 1;
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 */
%%
\n line_number++ column_number = 1;
. column_number++;
@ -17,8 +29,11 @@
"Boolean" {return T_BOOLEAN}
"character" {return T_CHARACTER}
/* KEYWARDS */
/* rules */
{DIGIT}+ {printf( "C_INTEGER: %s (%d)\n", yytext, atoi( yytext ) );}
"null" {printf( "C_NULL: %s (%d)\n", yytext, atoi( yytext ) );}
"while" {return WHILE}
"if" {return IF}
@ -30,5 +45,34 @@
"external" {return EXTERNAL}
"as" {return AS}
'{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 ) );}
/* OPERATORS */
"+" {return ADD;}
"-" {return SUB_OR_NEG;}
"*" {return MUL;}
"/" {return DIV;}
"%" {return REM;}
"<" {return LESS_THAN;}
"=" {return EQUAL_TO;}
":=" {return ASSIGN;}
"!" {return NOT;}
"&" {return AND;}
"|" {return OR;}
"." {return DOT;}
"reserve" {return RESERVE;}
"release" {return RELEASE;}
\"{SCHAR}*\" {printf( "C_STRING: %s (%d)\n", yytext, atoi( yytext ) );}
{STARTCOM}{COMMENTCHAR}*{ENDCOM} {printf( "COMMENT: %s (%d)\n", yytext, atoi( yytext ) );}
%%

4
simpleIntTest.txt Normal file
View File

@ -0,0 +1,4 @@
45
123
8392

46
simpleLiteralTest.a Normal file
View File

@ -0,0 +1,46 @@
"this is a string" 721398 'g' '/n' (* should print 3 tokens before this *)
'
'
12893 "this is not a string (*one valid token before this*)
(* spacey comment here
over multiple lines
will it work? *)
"
'''
'\'
false
(**)
'''
nullfalse
"nulltrue
null
'7'
true
'189
'\t'
'"'
'/'
'\n'
'\''
'\t'
'\\'
'n'
'\'
'fdsf'
(*/jnewjno2893u86^ Lots of random characters /n /t '") *)
'
'
' '
'''
"STRINGwithnotSPaces"
' '
'\ '
"J"
""
" "
\"\"
"{SCHAR}"
"SCHAR"
"[SCHAR]"
"FINAL: I'd think this is a legal \"string\" that contains \n \t several escaped characters, isn't it?"
"I'd think this is a legal \"string\" that contains several \\n \t escaped characters, isn't it?"

View File

@ -0,0 +1,23 @@
+
-
*
/
\
%
<
>
=
:=
=:
:
=
!
&
|
.
relEASE
release
RELEASE
reserve
RESERVE
reSERVe