fix lab-3 + add back else to our parsing
This commit is contained in:
parent
a3441f39c3
commit
8ab4a56aad
|
|
@ -25,6 +25,6 @@ int main(int argc, char *argv[]) {
|
|||
token_t t = yylex();
|
||||
if (t == TOKEN_EOF)
|
||||
break;
|
||||
printf("token: %d text: %s\n", t, yytext);
|
||||
printf("token: %d, text: %s\n", t, yytext);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,9 +53,6 @@ UTEST(scanner, hello) {
|
|||
|
||||
UTEST(scanner, quadratic) {
|
||||
struct token_st tokens[] = {
|
||||
{TOKEN_COMMENT, "*> Code altered from https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation"},
|
||||
{TOKEN_COMMENT, "*> Program finds the roots to a simple quadratic equation"},
|
||||
|
||||
{TOKEN_KEYWORD_IDENTIFICATION, "IDENTIFICATION"},
|
||||
{TOKEN_KEYWORD_DIVISION, "DIVISION"},
|
||||
{TOKEN_DOT, "."},
|
||||
|
|
@ -180,9 +177,8 @@ UTEST(scanner, quadratic) {
|
|||
{TOKEN_PROCEDURE, "PROCEDURE"},
|
||||
{TOKEN_KEYWORD_DIVISION, "DIVISION"},
|
||||
{TOKEN_DOT, "."},
|
||||
{TOKEN_COMMENT, "*> program begins here"},
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "'EQUATION: (1x^2) + 5x + 6 = 0'"},
|
||||
{TOKEN_STRING, "\"EQUATION: (1x^2) + 5x + 6 = 0\""},
|
||||
{TOKEN_KEYWORD_COMPUTE, "COMPUTE"},
|
||||
{TOKEN_IDENT, "discriminant"},
|
||||
{TOKEN_EQUAL, "="},
|
||||
|
|
@ -250,14 +246,14 @@ UTEST(scanner, quadratic) {
|
|||
{TOKEN_RIGHT_PARENTHESIS, ")"},
|
||||
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "'The equation has two distinct real roots: '"},
|
||||
{TOKEN_STRING, "\"The equation has two distinct real roots: \""},
|
||||
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "'Root 1: '"},
|
||||
{TOKEN_STRING, "\"Root 1: \""},
|
||||
{TOKEN_IDENT, "root1"},
|
||||
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "'Root 2: '"},
|
||||
{TOKEN_STRING, "\"Root 2: \""},
|
||||
{TOKEN_IDENT, "root2"},
|
||||
|
||||
// {TOKEN_EOF, ""},
|
||||
|
|
@ -282,17 +278,18 @@ UTEST(scanner, quadratic) {
|
|||
|
||||
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "'The equation has one real root: '"},
|
||||
{TOKEN_STRING, "\"The equation has one real root: \""},
|
||||
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "'Root: '"},
|
||||
{TOKEN_STRING, "\"Root: \""},
|
||||
{TOKEN_IDENT, "root1"},
|
||||
|
||||
|
||||
{TOKEN_ELSE, "ELSE"},
|
||||
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "'The equation has no real roots.'"},
|
||||
{TOKEN_STRING, "\"The equation has no real roots.\""},
|
||||
{TOKEN_END_IF, "END-IF"},
|
||||
|
||||
// {TOKEN_EOF, ""},
|
||||
|
||||
|
|
@ -403,7 +400,6 @@ UTEST(scanner, sorting) {
|
|||
{TOKEN_PROCEDURE, "PROCEDURE"},
|
||||
{TOKEN_KEYWORD_DIVISION, "DIVISION"},
|
||||
{TOKEN_DOT, "."},
|
||||
{TOKEN_COMMENT, "*> Initialize test data"},
|
||||
{TOKEN_MOVE, "MOVE"},
|
||||
{TOKEN_STRING, "\"30\""},
|
||||
{TOKEN_KEYWORD_TO, "TO"},
|
||||
|
|
@ -443,7 +439,6 @@ UTEST(scanner, sorting) {
|
|||
{TOKEN_INTEGER, "5"},
|
||||
{TOKEN_KEYWORD_TO, "TO"},
|
||||
{TOKEN_IDENT, "WS-SORT-MAX"},
|
||||
{TOKEN_COMMENT, "*> * Display original array"},
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "\"Original Array Contents:\""},
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
|
|
@ -470,7 +465,6 @@ UTEST(scanner, sorting) {
|
|||
{TOKEN_END_PERFORM, "END-PERFORM"},
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_SPACE, "SPACE"},
|
||||
{TOKEN_COMMENT, "*> * Simplified bubble sort"},
|
||||
{TOKEN_PERFORM, "PERFORM"},
|
||||
{TOKEN_VARYING, "VARYING"},
|
||||
{TOKEN_IDENT, "WS-I"},
|
||||
|
|
@ -540,7 +534,6 @@ UTEST(scanner, sorting) {
|
|||
{TOKEN_END_IF, "END-IF"},
|
||||
{TOKEN_END_PERFORM, "END-PERFORM"},
|
||||
{TOKEN_END_PERFORM, "END-PERFORM"},
|
||||
{TOKEN_COMMENT, "*> * Display sorted array"},
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
{TOKEN_STRING, "\"Sorted Array Contents:\""},
|
||||
{TOKEN_DISPLAY, "DISPLAY"},
|
||||
|
|
|
|||
BIN
lab-3/scanner
BIN
lab-3/scanner
Binary file not shown.
|
|
@ -1,6 +1,10 @@
|
|||
%{
|
||||
#include "token.h"
|
||||
%}
|
||||
%option warn
|
||||
%option nodefault
|
||||
%option yylineno
|
||||
|
||||
NAME [a-zA-Z]([a-zA-Z0-9_-]*[a-zA-Z0-9])?
|
||||
DIGIT [0-9]+
|
||||
%%
|
||||
|
|
@ -26,6 +30,7 @@ UNTIL { return TOKEN_UNTIL; }
|
|||
PERFORM { return TOKEN_PERFORM; }
|
||||
END-PERFORM { return TOKEN_END_PERFORM; }
|
||||
IF { return TOKEN_IF; }
|
||||
ELSE { return TOKEN_ELSE; }
|
||||
END-IF { return TOKEN_END_IF; }
|
||||
SPACE { return TOKEN_SPACE; }
|
||||
PIC { return TOKEN_PICTURE; }
|
||||
|
|
@ -42,8 +47,7 @@ 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; }
|
||||
|
|
@ -57,8 +61,9 @@ COMP-3 { return TOKEN_COMPUTATION_LEVEL_3; }
|
|||
"\'"[^']*"\'" { return TOKEN_STRING; }
|
||||
"(" { return TOKEN_LEFT_PARENTHESIS; }
|
||||
")" { return TOKEN_RIGHT_PARENTHESIS; }
|
||||
{NAME} { return TOKEN_IDENT; }
|
||||
{DIGIT} { return TOKEN_INTEGER; }
|
||||
|
||||
\. { return TOKEN_DOT; }
|
||||
|
||||
%%
|
||||
int yywrap() { return 1; }
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ typedef enum {
|
|||
TOKEN_IF,
|
||||
TOKEN_ELSE,
|
||||
TOKEN_END_IF,
|
||||
TOKEN_ELSE_IF,
|
||||
TOKEN_SPACE,
|
||||
TOKEN_KEYWORD_OCCURS,
|
||||
TOKEN_KEYWORD_VALUE,
|
||||
|
|
|
|||
|
|
@ -123,7 +123,6 @@ void stmt_print(struct stmt *s) {
|
|||
if (!s)
|
||||
return;
|
||||
|
||||
printf("stmt_print: %d\n", s->kind);
|
||||
|
||||
switch (s->kind) {
|
||||
case STMT_DECL:
|
||||
|
|
@ -148,11 +147,8 @@ void stmt_print(struct stmt *s) {
|
|||
case STMT_BLOCK:
|
||||
stmt_print(s->body);
|
||||
break;
|
||||
// we haven't implemented sections yet
|
||||
case STMT_SECTION:
|
||||
printf("section\n");
|
||||
printf("body: %p\n", s->body);
|
||||
printf("expr: %p\n", s->expr);
|
||||
printf("next: %p\n", s->next);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ int main(int argc, char *argv[]) {
|
|||
printf(
|
||||
"Enter an infix expression using the operators +-*/() ending with ;\n\n");
|
||||
|
||||
|
||||
if (yyparse() == 0) {
|
||||
printf("Parse successful: ");
|
||||
if (parser_result != NULL) {
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ statement : section
|
|||
| sect_data
|
||||
{$$ = $1;}
|
||||
| simple_stmt
|
||||
{$$ = stmt_create(STMT_BLOCK, NULL, NULL, NULL, NULL, $1, NULL, NULL);}
|
||||
{$$ = stmt_create(STMT_BLOCK, NULL, NULL, NULL, NULL, $1, NULL, NULL); printf("yytext1: %s\n", yytext);}
|
||||
| data_space
|
||||
{$$ = stmt_create(STMT_SECTION, NULL, NULL, NULL, NULL, NULL, NULL, NULL);}
|
||||
| data_declaration
|
||||
|
|
@ -117,14 +117,14 @@ type : TOKEN_KEYWORD_IDENTIFICATION
|
|||
| TOKEN_KEYWORD_DATA
|
||||
;
|
||||
simple_stmt : cbl_func_stmt
|
||||
{$$ = $1;}
|
||||
{$$ = $1; printf("yytext2: %s\n", yytext);}
|
||||
| if_branch
|
||||
| else_parts
|
||||
| perform_stmt
|
||||
;
|
||||
cbl_func_stmt : cbl_function
|
||||
| cbl_function op_parms
|
||||
{$$ = stmt_create($1->kind, NULL, NULL, $2, NULL, NULL, NULL, NULL);}
|
||||
{$$ = stmt_create($1->kind, NULL, NULL, $2, NULL, NULL, NULL, NULL); printf("yytext3: %s\n", yytext);}
|
||||
| cbl_function assignment_stmt
|
||||
| cbl_function op_parm assignment_stmt
|
||||
;
|
||||
|
|
@ -132,12 +132,12 @@ assignment_stmt : TOKEN_EQUAL op_parms
|
|||
| TOKEN_KEYWORD_TO op_parms
|
||||
;
|
||||
op_parms : op_parm
|
||||
{$$ = $1;}
|
||||
{$$ = $1; printf("yytext4: %s\n", yytext);}
|
||||
| op_parm op_parms
|
||||
{$$ = $1; $1->next_expr = $2;}
|
||||
;
|
||||
op_parm : mathmaticalexpr
|
||||
{$$ = $1;}
|
||||
{$$ = $1; printf("yytext5: %s\n", yytext);}
|
||||
| booleanexpr
|
||||
{$$ = $1;}
|
||||
;
|
||||
|
|
@ -151,7 +151,7 @@ math_op : TOKEN_ADD
|
|||
| TOKEN_EXPONENTIAL
|
||||
;
|
||||
mathmaticalexpr : type_expr
|
||||
{$$ = $1;}
|
||||
{$$ = $1; printf("yytext6: %s\n", yytext);}
|
||||
| mathmaticalexpr math_op term
|
||||
| container_expr
|
||||
{$$ = $1;}
|
||||
|
|
@ -169,7 +169,7 @@ type_expr : TOKEN_IDENT
|
|||
| TOKEN_INTEGER
|
||||
{$$ = expr_create_integer_literal(atoi(yytext));}
|
||||
| TOKEN_STRING
|
||||
{$$ = expr_create_string_literal(yytext);}
|
||||
{$$ = expr_create_string_literal(yytext); printf("yytext7: %s\n", yytext);}
|
||||
| TOKEN_SPACE
|
||||
{$$ = expr_create_integer_literal(0);}
|
||||
| TOKEN_SUB TOKEN_IDENT
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
%{
|
||||
#include "token.h"
|
||||
%}
|
||||
%option warn
|
||||
%option nodefault
|
||||
%option yylineno
|
||||
|
||||
NAME [a-zA-Z]([a-zA-Z0-9_-]*[a-zA-Z0-9])?
|
||||
DIGIT [0-9]+
|
||||
%%
|
||||
|
|
@ -26,6 +30,7 @@ UNTIL { return TOKEN_UNTIL; }
|
|||
PERFORM { return TOKEN_PERFORM; }
|
||||
END-PERFORM { return TOKEN_END_PERFORM; }
|
||||
IF { return TOKEN_IF; }
|
||||
ELSE { return TOKEN_ELSE; }
|
||||
END-IF { return TOKEN_END_IF; }
|
||||
SPACE { return TOKEN_SPACE; }
|
||||
PIC { return TOKEN_PICTURE; }
|
||||
|
|
@ -42,8 +47,7 @@ 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; }
|
||||
|
|
@ -57,8 +61,9 @@ COMP-3 { return TOKEN_COMPUTATION_LEVEL_3; }
|
|||
"\'"[^']*"\'" { return TOKEN_STRING; }
|
||||
"(" { return TOKEN_LEFT_PARENTHESIS; }
|
||||
")" { return TOKEN_RIGHT_PARENTHESIS; }
|
||||
{NAME} { return TOKEN_IDENT; }
|
||||
{DIGIT} { return TOKEN_INTEGER; }
|
||||
|
||||
\. { return TOKEN_DOT; }
|
||||
|
||||
%%
|
||||
int yywrap() { return 1; }
|
||||
Loading…
Reference in New Issue