Open mulderp opened 8 years ago
compilers are also translators from "high-level languages" to bits and bytes
in python: https://github.com/jayconrod/gypsum - nice examples of using lexers, combinators, ast,
in js, https://github.com/substack/node-falafel acorn ...
translation of code is related to recursively walking the abstract syntax tree. in fact recursion happens on a very small scale for expressions for example, the total program is then "combined" results from smaller recursions.
8cc compiler https://github.com/rui314/8cc and bflisp https://github.com/shinh/bflisp
if compiler writing is about recursion, the correct syntax is a question where the recursion breaks.
some jison tricks:
%lex
%%
(\r\n|\n)+ { print('eol'); return 'EOL'; }
[ \t]+ { print('w'); /* skip whitespace */}
\#[^\r\n]* { print('c'); /* skip comments */ }
/lex
%%
program
: lines EOF { }
;
lines
: /* empty */
| lines lines
;
line
: statement
| EOL
;
statement
: expr
;
expr
: 1
;
jison parsers from the browser with browserify transform: https://github.com/schmich/jisonify
arrays with jison http://stackoverflow.com/questions/28511760/making-arrays-in-jison
18:26] <> And PEG. :) [18:26] <> it is so cool indeed, it goes you closer to understanding the magic of computers [18:27] <> program: statement | statement program <-- BNF/yacc flavor [18:27] <> program = statement* <-- PEG [18:27] <> ahh, ok [18:27] <> Some of the syntax is nice, too. [18:27] <> i have seen that star in grammars [18:27] <> Though you may find writing your first expression parser... odd. [18:27] <> i was wondering what tool they use
Yes, it's called a Kleene star. https://en.wikipedia.org/wiki/Kleene_star interesting, indeed, with the BNF i had problems scanning for the newlines when i had build a small block statement with inner newlines for example "The" source for all things PEG: http://bford.info/packrat/
Your first attempt may not be too nice, but your second one probably will be. :) http://boost-spirit.com/home/
ah, i found this http://stackoverflow.com/questions/13367545/flex-isnt-generating-the-yyflexlexer-h-header it is a system lib
programs are based on interpretations of "abstract syntax tree" by computers.
that might give a good blog post incl references to:
James Halliday - What can you do with an AST? A whole lot Video for What can you do with an AST? A whole lot.▶ 18:52 https://www.youtube.com/watch?v=X8W2Le60up8
Most of the things you can do are terrible hacks but sometimes there is no other way. This talk will cover some ...
let's build a compiler:
http://compilers.iecc.com/crenshaw/