Git Product home page Git Product logo

basemax / compiler-university-project Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 0.0 231 KB

Implement an university compiler using Flex, Bison in the C programming language

License: GNU General Public License v3.0

Lex 0.67% Yacc 2.19% Shell 0.06% C 53.71% C++ 43.37%
c flex bison flex-bison flex-example bison-example bison-yacc bison-grammar compiler compiler-design compiler-frontend compilers-design university-project university-projects

compiler-university-project's Introduction

Compiler University Project

Implement an university compiler using Flex, Bison in the C programming language

Keep in mind that I am not a computer science or engeering student and do not need to do an university assignment.

It's just a fun project to test myself to see how much time I need to implement such a Uni Compiler.

The build.sh file is a shell script and you can use it to compile, create, test the compiler.

Tokens

/* */ \n program data procedure division get set put to execute repeat either both neither lt le gt ge ne eq not nor and or + - * / { } ( ) integer float char unsigned end , : ; 

You can find syntax example of this language at test.lang file.

Tech Stack

Lexer and parser build using Flex and Bison, other parts are pure C language.

For the dynamic strings I use a library called sds, it's a cool and tiny library for C.

Grammer and compiler syntax

%start program

program: structure
structure: heading data_part exe_part |
heading: PROGRAM ID COLON |
data_part: DATA DIVISION COMMANDEND data_body END COMMANDEND
data_body: data_stmt | data_stmt data_body
data_stmt: id_list COLON type COMMANDEND
id_list: ID | ID SEMICOLON id_list
type: INTEGER | FLOAT | CHAR {
exe_part: PROCEDURE DIVISION COMMANDEND stmt_list_or_no END COMMANDEND
stmt_list_or_no: stmt_list | 
stmt_list: stmt | stmt stmt_list
stmt: assgn_stmt | in_stmt | out_stmt | loop_stmt | cond1_stmt
assgn_stmt: SET ID TO expression COMMANDEND
in_stmt: GET in_list COMMANDEND
in_list: ID | ID SEMICOLON in_list
out_stmt: PUT out_list COMMANDEND | PUT COMMANDEND
out_list: ID SEMICOLON out_list | ID
loop_stmt: REPEAT SECTION_OPEN stmt_list_or_no SECTION_CLOSE condition
cond1_stmt: EXECUTE SECTION_OPEN stmt_list_or_no SECTION_CLOSE COMMANDEND
condition: EITHER expression COMMANDEND | NEITHER expression COMMANDEND | BOTH expression COMMANDEND
expression: SUB expression | NOT expression | expression GT expression | expression GE expression | expression LT expression | expression LE expression | expression AND expression | expression OR expression | ID | STRING | NUMBER | expression DIV expression | expression MUL expression | LEFTPAREN expression RIGHTPAREN | expression ADD expression | expression SUB expression

© Copyright Max Base

compiler-university-project's People

Contributors

basemax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

compiler-university-project's Issues

parser warnings

[max@base compiler-university-test]$ bash buid.sh

parser.y: warning: 3 shift/reduce conflicts [-Wconflicts-sr]
parser.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
parser.y: warning: shift/reduce conflict on token COMMANDEND [-Wcounterexamples]
  Example: SET ID TO STRING • COMMANDEND
  Shift derivation
    assgn_stmt
    ↳ SET ID TO STRING • COMMANDEND
  Example: SET ID TO STRING • COMMANDEND
  Reduce derivation
    assgn_stmt
    ↳ SET ID TO math_exp         COMMANDEND
                ↳ term
                  ↳ factor
                    ↳ element
                      ↳ STRING •
parser.y: warning: shift/reduce conflict on token AND [-Wcounterexamples]
Productions leading up to the conflict state found.  Still finding a possible unifying counterexample...  Example: BOTH elem1 • AND elem2 OR elem1 AND elem2
  Shift derivation
    condition
    ↳ BOTH logical_exp                      AND logical_exp
           ↳ logical_exp           OR elem1     ↳ elem1
             ↳ elem1                              ↳ elem2
               ↳ elem1 • AND elem2
  Example: BOTH elem1 • AND elem2 OR elem1 AND elem2
  Reduce derivation
    condition
    ↳ BOTH logical_exp AND logical_exp
           ↳ elem1 •       ↳ logical_exp OR elem1
                             ↳ elem1        ↳ elem1 AND elem2
                               ↳ elem2
parser.y: warning: reduce/reduce conflict on token OR [-Wcounterexamples]
  Example: EITHER logical_exp OR elem1 • OR elem1
  First reduce derivation
    condition
    ↳ EITHER logical_exp              OR logical_exp
             ↳ logical_exp OR elem1 •    ↳ elem1
  Example: EITHER logical_exp OR elem1 • OR elem1
  Second reduce derivation
    condition
    ↳ EITHER logical_exp OR logical_exp
                            ↳ logical_exp OR elem1
                              ↳ elem1 •
parser.y: warning: shift/reduce conflict on token AND [-Wcounterexamples]
Productions leading up to the conflict state found.  Still finding a possible unifying counterexample...time limit exceeded: 6.000000
  First example: logical_exp OR elem1 • AND elem2 AND logical_exp
  Shift derivation
    condition
    ↳ logical_exp                          AND logical_exp
      ↳ logical_exp OR elem1
                       ↳ elem1 • AND elem2
  Second example: logical_exp OR elem1 • AND elem2
  Reduce derivation
    condition
    ↳ logical_exp              AND elem2
      ↳ logical_exp OR elem1 •

realloc(): invalid pointer

[max@base compiler-university-test]$ bash buid.sh

parser.y: warning: 3 shift/reduce conflicts [-Wconflicts-sr]
parser.y: warning: 3 reduce/reduce conflicts [-Wconflicts-rr]
parser.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples

[max@base compiler-university-test]$ ./parser test.lang

realloc(): invalid pointer
Aborted (core dumped)

[max@base compiler-university-test]$ gdb ./parser

rGNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./parser...
(No debugging symbols found in ./parser)
(gdb) run test.lang 
Starting program: /home/max/OpenSource/compiler-university-test/parser test.lang
realloc(): invalid pointer

Program received signal SIGABRT, Aborted.
0x00007ffff7df1615 in raise () from /usr/lib/libc.so.6
(gdb) Quit
(gdb) 

[max@base compiler-university-test]$ ./parser test.lang

realloc(): invalid pointer
Aborted (core dumped)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.