项目作者: Raghav714

项目描述 :
Compiler
高级语言: Python
项目地址: git://github.com/Raghav714/RDDS-online-compiler.git
创建时间: 2018-11-04T12:32:11Z
项目社区:https://github.com/Raghav714/RDDS-online-compiler

开源协议:MIT License

下载


RDDS-online-compiler

The compiler is build from scratch with module :-

• Lexical analyser

• Parser

• Intermediate Code Generator

• Error handling

• Symbol table

LEXICAL ANALYSER AND SCANNER

• First program is run on the source file and then the code is broken into the list of distinct lines.

• at the start of each non-whitespace character, the frst character is used to determine the type of the
token to expect.

• The token is returned if the type is matched without any issue Otherwise, a scanner warning is thrown.

• The scanner warnings are never fatal, though syntactically the tokens returned may cause a parser error.

• For instance, if a string literal has no end quote a warning will be thrown and a quote will be assumed at the end of the line.

PARSER

• The left recursive from the grammar is removed.

• Type-checking is performed in expressions by returning the types from the expression tree functions and evaluating types for compatibility if an operation is performed.

• Parser resync points are used throughout the compiler to continue parsing if an error is encountered without propagating spurious error messages.

• Note that once a fatal error or any kind is encountered, code will no longer be generated.

INTERMEDIATE CODE GENERATOR

• Memory and registers for the operation of the program are defned and used as 32-bit integer arrays.

• All non-integer types present in the program are cast as integers for storage in the memory spaces.

• To ensure that pointers are 32-bit and may be cast to integer without issue, the gcc compiler fag -m32 is used.

• Future improvements could be made to “push back” register allocation to the frst register (R[0]) at the end of each scope.

LANGUAGE SYNTAXPROGRAM BODY

the program \ is

define \

body

\

fnish program

FUNCTION DEFINITION

function \(\ \ in, \ \ out) is

\

body

\finsh functione;

IF ELSE STATEMENT

if (\) then

\

else

\

finsh if

LOOP

for (counter = counter + 1e counter \<= num_loops)<\br>
\<\br>
fnish for<\br>

FUNCTION CALL

\(\,\)

DATA TYPE

• int – Integer

• bool – Boolean

• str – String

• foat – Float

FUNCTION

• putstr(“Enter 1st Integer:”)

• getint(variable)

• putint(result)

• getstr(“enter string”)

• getbool(testbool)

• putbool(testbool)

• getfoat(testfoat)

• putfoat(testfoat)

GRAMMAR

\ ::= \ \

\ ::= ‘ the’ ‘program’ \ ‘is’

\ ::= ‘ define’( \ ‘;’ ) ‘body’ ( \ ‘;’ ) ‘finish’ ‘program’

\ ::= [ ‘global’ ] \ [ ‘global’ ] \

\ ::=\ \ [ ‘[‘ \ ‘]’ ]

\ ::= ‘int’’ | ‘float’ | ‘bool’ | ‘str’

\ ::= \ \

\ ::=’function’ \ ‘(‘ [ \ ] ‘)’ ’is’

\ ::= ( \ ‘;’ ) ‘ body’ ( \<statement ‘;’ ) ‘finish’ ‘function’

\ ::= \ ‘,’ \ |\

\ ::= \ ( ‘in’ | ‘out’ )

\ ::= \ | \ | \ | \ |
\

\ ::= \ ‘=’ \

\ ::= ‘if’ ‘(‘ \ ‘)’ ‘then’ ( \ ‘;’ )+ [ ‘else’ ( \ ‘;’ )+ ]
‘finish’ ‘if’

\ ::= ‘for’ ‘(‘ \ ‘;’ \ ‘)’ ( \ ‘;’ ) ‘finish’ ‘for’

\ ::= \ ‘(‘ [ \ ] ‘)’

\ ::= \ ‘,’ \ |\

\ ::= \ [ ‘[‘ \ ‘]’ ]

\ ::= \ ‘&’ \ | \ ‘|’ \ | [ ‘not’ ] \

\ ::= \ ‘+’ \ | \ ‘-‘ \ |\

\ ::= \ ‘\<’ \ | \ ‘>‘ \ | \ ‘>=’ \ | \ ‘\<=’ \ |\ ‘==’ \ |\ ‘!=’ \ |\

\ ::= \
‘ \ | \ ‘/‘ \ | \

\ ::= ‘(‘ \ ‘)’ | [ ‘-‘ ] \ | [ ‘-‘ ] \ | \ | ‘true’ | ‘false’ |

\ ::= \ [ ‘[‘ \ ‘]’ ]

\ ::= [a-zA-Z][a-zA-Z0-9]*

\ ::= [0-9][0-9
][.[0-9_]]?

\ ::= “[a-zA-Z0-9 _,;:.’]*”

GUI

compiler1
compiler2

STEPS to run the program

$ sudo apt update

$ sudo apt install apache2

$ sudo apt-get install python3<\br>
$ sudo systemctl stop apache2.service

$ sudo systemctl start apache2.service

$ sudo systemctl enable apache2.service

$ sudo apt install php7.2 libapache2-mod-php7.2 php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-zip php7.2-curl

Copy the HTML, PHP, CSS, input.txt and test.src file in /var/www/html/ and type the localhost/index1.php in browser.

Edit the Path of the file compiler.py in index1.php

if the program doesnot work then run $ sudo chmod 777 * in /var/www/html/

if you wish to make RDDS as a command then open the bashrc file(sudo gedit ~/.bashrc) and write the command alias RDDS ='python3 /home/raghav/Downloads/compiler/2compiler/compiler.py -o OUT' in this you need to replace the path of compiler.py file.

use the command RDDS rdds_test/simple_add.src and ./a.out to see the output