Merge branch 'josh3' into 'main'

Josh3

See merge request lustje/language-interpreter-lab!6
This commit is contained in:
Riley Smith 2024-10-25 11:50:39 -07:00
commit 65fd85bf0b
3 changed files with 82 additions and 19 deletions

View File

@ -38,6 +38,7 @@ UTEST(scanner, hello) {
{TOKEN_EOF, ""}, {TOKEN_EOF, ""},
}; };
yyin = fopen("samples/hello-world.cbl", "r"); yyin = fopen("samples/hello-world.cbl", "r");
ASSERT_TRUE(yyin); ASSERT_TRUE(yyin);
int index = 0; int index = 0;

View File

@ -58,9 +58,32 @@ int yylex();
%token TOKEN_GREATER_THAN %token TOKEN_GREATER_THAN
%token TOKEN_LESS_THAN %token TOKEN_LESS_THAN
%token TOKEN_EXPONENTIAL %token TOKEN_EXPONENTIAL
%token TOKEN_DISPLAY
%% %%
file : statements
statements : statements statement
| statement
;
statement : section
| sect_data
| simple_stmt
;
section : type TOKEN_KEYWORD_DIVISION TOKEN_DOT
| type TOKEN_RUN TOKEN_DOT
;
sect_data : TOKEN_PROGRAM_ID TOKEN_DOT TOKEN_IDENT TOKEN_DOT
;
type : TOKEN_IDENTIFICATION
| TOKEN_PROCEDURE
| TOKEN_STOP
;
simple_stmt : function
;
function : TOKEN_DISPLAY parms
;
parms : TOKEN_STRING
;
%% %%

View File

@ -1,25 +1,64 @@
%{ %{
#include "token.h" #include "token.h"
%} %}
NAME [a-zA-Z]([a-zA-Z0-9_-]*[a-zA-Z0-9])?
%option nounput DIGIT [0-9]+
%option noinput
%option yylineno
DIGIT [0-9]
LETTER [a-zA-Z]
%% %%
(" "|\t|\n) /* skip whitespace */ (" "|\t|\n) /* skip whitespace */
\+ { return TOKEN_PLUS; } \*>\ ?.* { return TOKEN_COMMENT; }
\- { return TOKEN_MINUS; } IDENTIFICATION { return TOKEN_IDENTIFICATION; }
\* { return TOKEN_MUL; } DIVISION { return TOKEN_KEYWORD_DIVISION; }
\/ { return TOKEN_DIV; } PROGRAM-ID { return TOKEN_PROGRAM_ID; }
\( { return TOKEN_LPAREN; } PROCEDURE { return TOKEN_PROCEDURE; }
\) { return TOKEN_RPAREN; } DATA { return TOKEN_KEYWORD_DATA; }
\; { return TOKEN_SEMI; } SECTION { return TOKEN_KEYWORD_SECTION; }
{DIGIT}+ { return TOKEN_INT; } WORKING-STORAGE { return TOKEN_WORKING_STORAGE; }
. { return TOKEN_ERROR; } DISPLAY { return TOKEN_DISPLAY; }
%% STOP { return TOKEN_STOP; }
RUN { return TOKEN_RUN; }
MOVE { return TOKEN_MOVE; }
TO { return TOKEN_KEYWORD_TO; }
VARYING { return TOKEN_VARYING; }
FROM { return TOKEN_KEYWORD_FROM; }
BY { return TOKEN_KEYWORD_BY; }
UNTIL { return TOKEN_UNTIL; }
PERFORM { return TOKEN_PERFORM; }
END-PERFORM { return TOKEN_END_PERFORM; }
IF { return TOKEN_IF; }
END-IF { return TOKEN_END_IF; }
SPACE { return TOKEN_SPACE; }
PIC { return TOKEN_PICTURE; }
OCCURS { return TOKEN_KEYWORD_OCCURS; }
VALUE { return TOKEN_KEYWORD_VALUE; }
COMPUTE { return TOKEN_KEYWORD_COMPUTE; }
FUNCTION { return TOKEN_KEYWORD_FUNCTION; }
X { return TOKEN_ALPHANUMERIC; }
S9 { return TOKEN_SIGNED_NUMERIC; }
9 { return TOKEN_NUMERIC; }
V9 { return TOKEN_IMPLIED_DECIMAL; }
COMP { return TOKEN_COMPUTATION_LEVEL_0; }
COMP-1 { return TOKEN_COMPUTATION_LEVEL_1; }
COMP-2 { return TOKEN_COMPUTATION_LEVEL_2; }
COMP-3 { return TOKEN_COMPUTATION_LEVEL_3; }
{DIGIT} { return TOKEN_INTEGER; }
{NAME} { return TOKEN_IDENT; }
\+ { return TOKEN_ADD; }
\- { return TOKEN_SUB; }
\*\* { return TOKEN_EXPONENTIAL; }
\* { return TOKEN_MULTIPLY; }
\/ { return TOKEN_DIVIDE; }
\> { return TOKEN_GREATER_THAN; }
\< { return TOKEN_LESS_THAN; }
\= { return TOKEN_EQUAL;}
"\""[^"]*"\"" { return TOKEN_STRING; }
"\'"[^']*"\'" { return TOKEN_STRING; }
"(" { return TOKEN_LEFT_PARENTHESIS; }
")" { return TOKEN_RIGHT_PARENTHESIS; }
\. { return TOKEN_DOT; }
%%
int yywrap() { return 1; } int yywrap() { return 1; }