diff --git a/labs/03/analyzer.l b/labs/03/analyzer.l new file mode 100644 index 0000000..f7c8184 --- /dev/null +++ b/labs/03/analyzer.l @@ -0,0 +1,26 @@ +/* Created by Hedguhar Domínguez on 29/01/2024. */ +/* Lexical analyzer for the compiler */ + + +%{ + +#include "analyzer.tab.h" /* Header file generated by Bison */ + +%} + + +%% + +"a" { return A; } +"the" { return THE; } +"boy" { return BOY; } +"girl" { return GIRL; } +"flower" { return FLOWER; } +"touches" { return TOUCHES; } +"likes" { return LIKES; } +"sees" { return SEES; } +"with" { return WITH; } +[ \t\n] { } +. { return OTHER; } + +%% diff --git a/labs/03/analyzer.y b/labs/03/analyzer.y new file mode 100644 index 0000000..a44ab99 --- /dev/null +++ b/labs/03/analyzer.y @@ -0,0 +1,89 @@ +/* Created by Hedguhar Domínguez on 29/01/2024. */ +/* Syntactic analyzer for the compiler */ + + +%{ + +#include +#include + +// Lex variables +extern int yylex(); + +// Function prototypes +int yyerror(char const * s); + +%} + + +/* Terminal symbols */ +%token A THE BOY GIRL FLOWER TOUCHES LIKES SEES WITH OTHER +%start sentence + + +%% + +// Grammar rules + +// ⟨SENTENCE⟩ → ⟨NOUN-PHRASE⟩⟨VERB-PHRASE⟩ +sentence: noun_phrase verb_phrase { printf("PASS\n"); } +; + +// ⟨NOUN-PHRASE⟩ → ⟨CMPLX-NOUN⟩ | ⟨CMPLX-NOUN⟩⟨PREP-PHRASE⟩ +noun_phrase: cmplx_noun + | cmplx_noun prep_phrase +; + +// ⟨VERB-PHRASE⟩ → ⟨CMPLX-VERB⟩ | ⟨CMPLX-VERB⟩⟨PREP-PHRASE⟩ +verb_phrase: cmplx_verb + | cmplx_verb prep_phrase +; + +// ⟨PREP-PHRASE⟩ → ⟨PREP⟩⟨CMPLX-NOUN⟩ +prep_phrase: prep cmplx_noun +; + +// ⟨CMPLX-NOUN⟩ → ⟨ARTICLE⟩⟨NOUN⟩ +cmplx_noun: article noun +; + +// ⟨CMPLX-VERB⟩ → ⟨VERB⟩ | ⟨VERB⟩⟨NOUN-PHRASE⟩ +cmplx_verb: verb + | verb noun_phrase +; + +// ⟨ARTICLE⟩ → a | the +article: A + | THE +; + +// ⟨NOUN⟩ → boy | girl | flower +noun: BOY + | GIRL + | FLOWER +; + +// ⟨VERB⟩ → touches | likes | sees +verb: TOUCHES + | LIKES + | SEES +; + +// ⟨PREP⟩ → with +prep: WITH +; + +%% + + +// Error function +int yyerror(char const * s) +{ + fprintf(stderr, "FAIL\n"); +} + + +void main() +{ + yyparse(); +}