parse tools in erlang

Upload: leafo

Post on 04-Apr-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 Parse Tools in Erlang

    1/13

    leex and yecc, Parse tools forErlang

    Leaf Corcoran, 2011

  • 7/29/2019 Parse Tools in Erlang

    2/13

    Overview of Erlang

    Functional languageSingle assignment on variablesDynamic typingPattern matching: functions, conditionals, assignment

    Excellent concurrency with message passingHigh fault tolerance, "Let it crash"Modules can be hot loaded

    OTP standard library

  • 7/29/2019 Parse Tools in Erlang

    3/13

    Overview of Erlang Syntax2323. % numbers

    564 + 34.5.

    Var = 34. % variablesVar = 22. % ERROR

    {tuple, "Hello", 'World', '$money'}.

    "hello" == [104, 101, 108, 108, 111].

    [X || X

  • 7/29/2019 Parse Tools in Erlang

    4/13

    Overview of Erlang Modules% stuff.erl

    -module(stuff).-export([get_name/1]).

    get_name({person, {name, Name}, _}) ->io:format("~w~n", Name);

    get_name(_) ->error("Don't understand").

    1> c(stuff).{ok,stuff}

    2> stuff:get_name({person, {name, "Leaf"}, {age, 22}}).Leaf

  • 7/29/2019 Parse Tools in Erlang

    5/13

    Overview of Erlang Misc% fibonacci

    fib(1) -> 0;fib(2) -> 1;fib(N) when N > 0 -> fib(N-1) + fib(N-2).

    % quicksort

    qsort([Pivot, Rest]) ->qsort([ X || X

  • 7/29/2019 Parse Tools in Erlang

    6/13

    Scanner/parser communicationThe scanner is a function that takes string and returns list of

    tokens.The parser is a function that takes list of tokens (or afunction that generates tokens) and returns a parse tree.

    leex: scanner generator

    yecc: parser generator

    They come with OTP, standard distribution.

  • 7/29/2019 Parse Tools in Erlang

    7/13

    Scanner/parser communicationFor Yecc, the list of tokens is a list of tuples containing token,

    line, and optionally a value.

    scanner:scan("(5 + 2) - 3").

    [{'(', 1},{number, 1, 5},{'+', 1},{number, 1, 2},{')', 1},{'-', 1},

    {number, 1, 3},{'$end', 1}

    ]

  • 7/29/2019 Parse Tools in Erlang

    8/13

    yecc parser generatorLALR-1 Parser, based on same algorithm as yacc.

    Matches against patterns of Erlang atoms, and runsassociated action.

    Action is Erlang code.Evaluate directly, eg. to make a simple calculator,Can build tree nodes.

    Has access to matched tokens using $ syntax.Has following sections

    HeaderTerminals, Nonterminals

    Grammar rulesErlang code

    Creates a module that exports:parse/1parse_and_scan/1

  • 7/29/2019 Parse Tools in Erlang

    9/13

    leex scanner generatorMaps regular expressions to Erlang code.

    Result of code determines whether to generate token,skip data, or throw an error

    Can also push characters back on streamHas three sections:

    Definitions

    RulesErlang code

    Creates a module that exports:string/1

    token/2, tokens/2

  • 7/29/2019 Parse Tools in Erlang

    10/13

    Writing an interpreterEvaluate tokens by pattern matching on their type.

    Pass environment as function argument.

  • 7/29/2019 Parse Tools in Erlang

    11/13

    Message passingVery cheap to create processes, no shared data.

    Pass around PID to keep track of processes.Sent messages are stored in mailbox.Receive block lets a process pattern match on messages.

    -module(server).

    -export([loop/0]).

    loop() ->receive

    {print, Msg} -> io:format("~p~n", [Msg])end,

    loop().

    1> Pid = spawn(server, loop, []).

    2> Pid ! {print, "Hello world!"}.

  • 7/29/2019 Parse Tools in Erlang

    12/13

    ConclusionError messages aren't good

    SometimesThe grammar and parser can be verbose.Small learning curve.Documentation is decent but lacking.

    Download code and presentation at: http://leafo.net/erlang

    http://leafo.net/erlang
  • 7/29/2019 Parse Tools in Erlang

    13/13

    Things to check outReia

    http://reia-lang.org/Ruby-like scripting language that runs on the Erlang virtualmachine.Efenehttp://marianoguerra.com.ar/efene/

    Programming language with C-like syntax that runs onErlang platform.Erjanghttp://www.erjang.org/

    JVM implementation of Erlang virtual machine.

    http://www.erjang.org/http://marianoguerra.com.ar/efene/http://www.erjang.org/http://marianoguerra.com.ar/efene/http://reia-lang.org/