From 4ba4adfb8d41833ab32fcd1ca111ff18c98abecf Mon Sep 17 00:00:00 2001 From: Dimitar Trendafilov Date: Thu, 27 Sep 2018 01:50:24 +0300 Subject: Expanded the grammars --- website/content/slides/01_dragons/_index.md | 90 +++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 6 deletions(-) diff --git a/website/content/slides/01_dragons/_index.md b/website/content/slides/01_dragons/_index.md index df52f22..b311f65 100644 --- a/website/content/slides/01_dragons/_index.md +++ b/website/content/slides/01_dragons/_index.md @@ -109,13 +109,68 @@ Recognizes the rules from stream of tokens and builds the AST of the program. - parser generation tool --- -###### Parser +###### Grammars -# TODO: Expand +The grammar of a languages defines the structure of correct sentences and how to +derive their meaning. + +--- +###### CFG + +Context Free Grammars + + := | () | + := + | - | * | / + := [0-9]+ + +--- +###### PEG + +Parser Expression Grammars are similar to CFG, but are more convinient for +parsing, since the `|` operator is not ambigious. + +- CFG allows selecting any matching variant and can have more than one parse + tree. + - parsing algorithms try to resolve this +- PEG pritizes the variants in the order that they are written. + +--- +###### PEG + + := + := ([+-] )* + := ([*/] )* + := [0-9]+ / '(' ')' + +- That is a CFG as well, so for some grammars there is no difference. +- Some languages can be expressed only with ambigious grammars. + +--- +###### CFG vs PEG + +Where the else goes? + + := if else + | if + + + if x0 if x1 s1 else s2 + + if x0 { if x1 s1 } else s2 // 1 + if x0 { if x1 s1 else s2 } // 2 + +- CFG - it is ambigious +- PEG - 2 - because it is the first option + +--- +###### LALR(n) + +LR(n) and LALR(n) are family of parsing algorithms for CFG + +- n is the number of look ahead terminals +- L - *left-to-right*, +- R - *right-most derivation* - the right most non-terminal is replaced -- CFG -- PEG -- LALR(n) --- ###### Program @@ -158,6 +213,8 @@ that is convinient for: - Static Single Assignment form +# TODO: expand + --- ### IR in LLVM @@ -191,7 +248,26 @@ that is convinient for: - AOT - less overhead, faster startup, generally better performance - JIT - better chance for optimizations, since some of the program arguments - will be contstants once the program has started + will be constants once the program has started, some of the virtual calls can + be devirtuallized at runtime + +--- +## Transpiler + +> Takes program written in one language and translates that to another language. + +- cfront +- CoffeScript, TypeScript +- nim + +--- +## Code transformation tools + +Lots of tools can be created based on the AST representation of a program + +- code formatting - *clang-format*, *gofmt*, *autopep8* +- code transformation - *clang-modernize* +- code analysis - *clang-tidy* --- ## Virtual Machine @@ -210,6 +286,8 @@ collection and most of the standard library of the language. --- ### RPN as an interperter +# TODO: expand + --- ## What is a machine? -- cgit v1.3.1-23-gd82d