Skip to content

Instantly share code, notes, and snippets.

@AloyASen
Created September 30, 2017 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AloyASen/c1a783920e901db284f0a79ebccf025c to your computer and use it in GitHub Desktop.
Save AloyASen/c1a783920e901db284f0a79ebccf025c to your computer and use it in GitHub Desktop.
Basic System parser Flex , bison , g++ , ubuntu -linux

all these files are developed on linux and are ready to be extensible.

the folder structure suggested {project root}

/Readme.md

{bin}-for storing the compiled output {test}-for storing the scripts upon which the compiled system is to be tested

./(^)/enter.code

{compiler}-contains all the source files

./(^).lex.l

..grammer.y

..main.cc

..Makefile

Packages required to build this module >> flex , bison

[[ use sudo , or yum to install it onto your system ]]

enter: -> {
}
%{
#include <stdio.h>
void yyerror(char *);
int yylex(void);
int sym[26];
%}
%token NAME COLON RIGHT_ARROW LEFT_BRACE RIGHT_BRACE
%define api.value.type {double}
%start input
%%
input:
NAME COLON RIGHT_ARROW LEFT_BRACE RIGHT_BRACE
|%empty
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}
%option noyywrap
%{
#include "grammer.tab.h"
%}
alpha [a-zA-Z]
name {alpha}([0-9]*{alpha}*)+
whitespace [ \r\f\v\t]
linefeed \n
%%
{name} {printf("LEX:: name \n");return NAME;}
":" {printf("LEX:: COLON \n");return COLON;}
"->" {printf("LEX:: RIGHT_ARROW \n");return RIGHT_ARROW;}
"{" {printf("LEX:: LEFT_BRACE \n");return LEFT_BRACE;}
"}" {printf("LEX:: RIGHT_BRACE \n");return RIGHT_BRACE;}
{whitespace}
{linefeed} ++yylineno;
%%
#include <iostream>
extern "C"
{
void yyerror(const char *);
int yyparse();
int yylex();
}
int main()
{
int result =yyparse();
if (result)
{
std::cout<< "the input is valid"<<std::endl;
}else{
std::cout<< "the input is valid"<<std::endl;
}
return result;
}
all:
$(MAKE) grammer
$(MAKE) lex
gcc -c compiler/grammer.tab.c compiler/lex.yy.c
mv *.o compiler
ar rvs compiler/lexgram.a compiler/grammer.tab.o compiler/lex.yy.o
g++ -o bin/freamps -Wall -Wextra compiler/main.cc compiler/lexgram.a
grammer:
bison -d compiler/grammer.y
mv grammer.tab.* compiler
lex:
flex compiler/lex.l
mv lex.yy.c compiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment