Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions labs/03/analyzer.l
Original file line number Diff line number Diff line change
@@ -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; }

%%
89 changes: 89 additions & 0 deletions labs/03/analyzer.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* Created by Hedguhar Domínguez on 29/01/2024. */
/* Syntactic analyzer for the compiler */


%{

#include <stdio.h>
#include <stdlib.h>

// 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();
}