Skip to content

Commit

Permalink
Merge pull request #13 from tomasanchez/CFG-13-parser
Browse files Browse the repository at this point in the history
Cfg 13 parser
  • Loading branch information
tomasanchez authored Apr 17, 2022
2 parents b8053f8 + ec1550a commit 2d935ef
Show file tree
Hide file tree
Showing 19 changed files with 2,234 additions and 239 deletions.
34 changes: 24 additions & 10 deletions console/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# C Compiler
CC = gcc
# Compiler Flags
CFLAGS = -Wall -Wextra -g3
CFLAGS = -Wall -Wno-unused-function -Wextra -g3
# Test Compiler flags
TCFLAGS = -Wall -Wextra -Wshadow -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-variable -Wno-pragmas -O3 -g3
# Used libraries
Expand Down Expand Up @@ -51,22 +51,36 @@ LEAKS = $(LOG_FOLDER)/$(MODULE_NAME)_leaks.log
# Thread chek log file
HELGRIND = $(LOG_FOLDER)/$(MODULE_NAME)_threads.log

# Flex/Lex.
LEXC=flex
LEX=flex/console.l
# Flex/Lex
FLEX = flex
FFLAGS = -l
FLEX_FILES = ./flex/*.l

all : flex compile run
# Bison
BISON = bison
BFLAGS = -d
BISON_FILES = ./bison/*.y

.PHONY: all flex
all : flex bison compile run

.PHONY: all flex bison

# ! Avoid modifying this section - (Unless you know what you are doing) --------------------------------------------------------------
# Lexer Generator
# Scanner Generator
flex:
@echo "Generating LExer - Needs FLEX installed..."
@echo "Generating SCANNER - Needs FLEX installed..."
@mkdir -p src/console
@mkdir -p include/console
$(FLEX) $(FFLAGS) $(FLEX_FILES)
@echo "Built console's scanner"

# Parser Generator
bison:
@echo "Generating PARSER - Needs BISON installed..."
@mkdir -p src/console
@mkdir -p include/console
$(LEXC) $(LEX)
@echo "Built console's lexer"
$(BISON) $(BFLAGS) $(BISON_FILES)
@echo "Built console's scanner"

# Compilation rule
# ? Generates .out executable
Expand Down
45 changes: 45 additions & 0 deletions console/bison/parser.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%code top{
#include <stdio.h>

#include "scanner.h"
#include "semantic.h"
}

%code provides{
void yyerror(const char *);
extern int yylexerrs;
}

%defines "./include/console/parser.h"
%output "./src/console/parser.c"

%define api.value.type {char *}
%define parse.error verbose

%token CONSTANT EXIT NO_OP READ IO WRITE COPY

%start instructions

%%

instructions
: instruction
| instructions instruction
;

instruction
: EXIT { request_exit(); }
| NO_OP CONSTANT { request_no_op($2); }
| IO CONSTANT { request_io($2); }
| READ CONSTANT { request_read($2); }
| WRITE CONSTANT CONSTANT { request_write($2, $3); }
| COPY CONSTANT CONSTANT { request_copy($2, $3); }
;

%%

/* Informa la ocurrencia de un error. */
void yyerror(const char *s){
printf("línea #%d: %s\n", yylineno, s);
return;
}
6 changes: 6 additions & 0 deletions console/entrada.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NO_OP 5
I/O 3000
READ 0
WRITE 4 42
COPY 0 3
EXIT
77 changes: 39 additions & 38 deletions console/flex/console.l
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
/* Auto-Generated Header file name */
%option header-file = "include/console/lexer.h"
/* Auto-generated Source file name */
%option outfile = "src/console/lexer.c"
/* 8-bit Scanner (THREAD-SAFE)*/
%option 8bit noyywrap
/*For "include"*/
%{

#include <stdio.h>
#include "parser.h"

#define ERROR_LEXICO(mensaje) yylexerrs++; char buffer[8229]; sprintf(buffer, "Error léxico: %s: %s", mensaje, yytext ); yyerror(buffer);

%}

%option outfile="./src/console/scanner.c"
%option header-file="./include/console/scanner.h"

%option 8bit
%option stack
/* Case insensitve option*/
%option case-insensitive
/* Count line number*/
%option noyywrap
%option nounput
%option noinput
%option yylineno
%option case-insensitive

/* C/C++ code included inside %top{} are copied to both the `_lexer.c` and the `_lexer.h` */
%top{
}
space [[:space:]]

/* C/C++ code included inside %{%} is copied to `_lexer.c` but not to the header-file*/
%{
/**
* console_lexer.c
*
* @file Auto-generated lexer by Flex.
* @author Tomás Sánchez
* @since 04.14.2022
*/

#include <stdlib.h>

// Avoiding warning implicit-function-declaration as fileno is no std in C
extern int fileno(FILE *);
%}
invalid_character [~`!@#$^&\[}[\]'"?\\>]

alpha [[:alpha:]]

digit [[:digit:]]

constant {digit}+

%%
[ \t]
("NO_OP") { puts("No operation");}
("I/O") { puts("Input/Output");}
("READ") { puts("Read");}
("COPY") { puts("Copy");}
("WRITE") { puts("Write");}
("Exit") { puts("Exit");}
[\n] { puts("End of line");}
[0-9]+ { puts("Number");}
. { puts("Invalid symbol");}

"NO_OP" { return NO_OP;}
"I/O" { return IO;}
"READ" { return READ;}
"COPY" { return COPY;}
"WRITE" { return WRITE;}
"EXIT" { return EXIT;}

{space}+ {} /* ignore new lines, tabulations and spaces */

{constant} { yylval = strdup(yytext); return CONSTANT; }
{constant}({invalid_character}|{alpha})({invalid_character}|{alpha}|{digit})* { ERROR_LEXICO("constante inválida"); }

. { ERROR_LEXICO("caracter desconocido"); }

%%
2 changes: 1 addition & 1 deletion console/include/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int on_before_exit(void);
*
* @return ERROR o SUCCESS
*/
int on_client_run(void);
int on_client_run(char *instructions_file_name, int process_size);

/**
* @brief Event handler de establecer conexion
Expand Down
89 changes: 89 additions & 0 deletions console/include/console/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* A Bison parser, made by GNU Bison 3.8.2. */

/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 Free Software Foundation,
Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */

/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */

/* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual,
especially those whose name start with YY_ or yy_. They are
private implementation details that can be changed or removed. */

#ifndef YY_YY_INCLUDE_CONSOLE_PARSER_H_INCLUDED
# define YY_YY_INCLUDE_CONSOLE_PARSER_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif

/* Token kinds. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
enum yytokentype
{
YYEMPTY = -2,
YYEOF = 0, /* "end of file" */
YYerror = 256, /* error */
YYUNDEF = 257, /* "invalid token" */
CONSTANT = 258, /* CONSTANT */
EXIT = 259, /* EXIT */
NO_OP = 260, /* NO_OP */
READ = 261, /* READ */
IO = 262, /* IO */
WRITE = 263, /* WRITE */
COPY = 264 /* COPY */
};
typedef enum yytokentype yytoken_kind_t;
#endif

/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef char * YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1
#endif


extern YYSTYPE yylval;


int yyparse (void);

/* "%code provides" blocks. */
#line 8 "./bison/parser.y"

void yyerror(const char *);
extern int yylexerrs;

#line 88 "./include/console/parser.h"

#endif /* !YY_YY_INCLUDE_CONSOLE_PARSER_H_INCLUDED */
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define yyHEADER_H 1
#define yyIN_HEADER 1

#line 6 "include/console/lexer.h"
#line 5 "./include/console/scanner.h"

#line 8 "include/console/lexer.h"
#line 7 "./include/console/scanner.h"

#define YY_INT_ALIGNED short int

Expand Down Expand Up @@ -206,13 +206,10 @@ void yyfree ( void * );
#define yywrap() (/*CONSTCOND*/1)
#define YY_SKIP_YYWRAP

#define YY_FLEX_LEX_COMPAT
extern int yylineno;

extern char *yytext;
#ifdef yytext_ptr
#undef yytext_ptr
#endif
#define yytext_ptr yytext
extern char yytext[];

#ifdef YY_HEADER_EXPORT_START_CONDITIONS
#define INITIAL 0
Expand Down Expand Up @@ -469,9 +466,9 @@ extern int yylex (void);
#undef yyTABLES_NAME
#endif

#line 46 "flex/console.l"
#line 47 "./flex/console.l"


#line 476 "include/console/lexer.h"
#line 472 "./include/console/scanner.h"
#undef yyIN_HEADER
#endif /* yyHEADER_H */
13 changes: 13 additions & 0 deletions console/include/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#define CONTEXT_AVAILABLE 0
#define CONTEXT_UNAVAILABLE -1

typedef struct context
{
char *input_file_name;
int process_size;
int status;
} context_t;

context_t read_context(int argc, char *argv[]);
40 changes: 40 additions & 0 deletions console/include/semantic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* template.c
*
* Copyright 2020 Roberto Nicolás Savinelli <rnsavinelli@est.frba.utn.edu.ar>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/

#ifndef SEMANTIC_H_
#define SEMANTIC_H_

#include "parser.h"

void request_exit(void);

void request_no_op(char *constant);

void request_io(char *constant);

void request_read(char *constant);

void request_write(char *constant_l, char *constant_r);

void request_copy(char *constant_l, char *constant_r);

#endif // SEMANTIC_H_
Loading

0 comments on commit 2d935ef

Please sign in to comment.