Follow me on twitter: Ajay Halthor (@ajhalthor) / ajhalthor
Get the code for the entire project : https://github.com/ajhalthor/interpreter
INSTALL FLEX & BISON
linux: http://ccm.net/faq/30635-how-to-insta...
macOS: http://stackoverflow.com/questions/31...
Windows: http://techapple.net/2014/07/flex-win...
SYNTAX HIGHLIGHT FOR SUBLIME TEXT
Flex : https://github.com/textmate/lex-flex....
Bison : https://bitbucket.org/artyom_smirnov/...
NOTES
In this video, we are going to create our own little compiler to validate the declaration and initialization section of a C program. We’ll use 2 language tools: Lex and Yacc (or flex and bison) to accomplish this.
In this video, we are going to
1. install flex and bison
2. install syntax highlighting for flex and bison in sublime
3. Explain the code files
4. and run this output on a sample file.
The lex file has 3 main parts, separated by “double percentage signs”:
The first part is a list of harder files and function definitions encapsulated in “percentage-curly braces”. The second part has the list of acceptable tokens and the final is for some C user defined functions.
Like the lex file, yacc file also consists of 3 sections. The first part is a list of harder files and function definitions encapsulated in “percentage-curly braces”. In between the first and second sections, we define the nature of the error message generated by YACC. The second section has the Context free Grammar and the 3rd has User defined functions.
Langfunctions.h :
Data_Type : character array that holds the data_type for the current declaration statement.
noOfIdentifiers : number of identifiers in the input file.
clearBuffers() : to clear the value of the datatype stored.
storeDataType() : to store the datatype of the current declaration statement.
retrieveDataType(): Created to make things look uniform.
isDuplicate() : checks if the newly encountered identifier has already been declared before.
extractIdentifier() : extracts the name of the array.
storeIdentifier() : add the encountered identifier to the list of identifiers.
AssignmentError() is called in case an invalid assignment is made
DuplicateIdentifierError() is called if the isDuplicate() function returns True in the yacc file.
validators.h :
isValidAssignment() : checks if the datatype which we pass in from the later part of the yacc file is the same as the current datatype of the identifier.
itoa, ftoa and ctoa are used to convert integers, floating point numbers and characters into ascii type.
Run the Program:
To execute this code, Go to your terminal, enter your working directory and type 4 commands.
$ yacc -d syntax2.y
$ lex semantics.l
$ cc lex.yy.c y.tab.c -o output
$ ./output [angle bracket] sample
Now that we have the brains of the program setup, I’m gonna try to create a cool GUI in the next video so that it'll actually look like an application. Keep an eye out for that video.
If you are new to the channel hit that subscribe button on your way out. Lets see if we can get 10 likes on this video (cmon!!!). PLEEEESSSSEEE. I’ll be a happy dude.
:)
Watch video Lets make a Programming Language! | Create Programming Language #2 online without registration, duration hours minute second in high quality. This video was added by user CodeEmporium 13 March 2017, don't forget to share it with your friends and acquaintances, it has been viewed on our site 46,177 once and liked it 1.1 thousand people.