├── .gitignore ├── LICENSE ├── README.md ├── config └── config.exs ├── lib └── leex_yecc_tutorial.ex ├── mix.exs ├── src ├── number_lexer.xrl └── number_parser.yrl └── test ├── leex_yecc_tutorial_test.exs └── test_helper.exs /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | erl_crash.dump 5 | *.ez 6 | /src/number_lexer.erl 7 | /src/number_parser.erl 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Cameron Price 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeexYeccTutorial 2 | 3 | This is the code to support [this article](http://cameronp.svbtle.com/how-to-use-leex-and-yecc) on using leex and yecc with elixir. 4 | 5 | This is NOT PRODUCTION code. 6 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | use Mix.Config 4 | 5 | # This configuration is loaded before any dependency and is restricted 6 | # to this project. If another project depends on this project, this 7 | # file won't be loaded nor affect the parent project. For this reason, 8 | # if you want to provide default values for your application for 9 | # 3rd-party users, it should be done in your "mix.exs" file. 10 | 11 | # You can configure for your application as: 12 | # 13 | # config :leex_yecc_tutorial, key: :value 14 | # 15 | # And access this configuration in your application as: 16 | # 17 | # Application.get_env(:leex_yecc_tutorial, :key) 18 | # 19 | # Or configure a 3rd-party app: 20 | # 21 | # config :logger, level: :info 22 | # 23 | 24 | # It is also possible to import configuration files, relative to this 25 | # directory. For example, you can emulate configuration per environment 26 | # by uncommenting the line below and defining dev.exs, test.exs and such. 27 | # Configuration from the imported file will override the ones defined 28 | # here (which is why it is important to import them last). 29 | # 30 | # import_config "#{Mix.env}.exs" 31 | -------------------------------------------------------------------------------- /lib/leex_yecc_tutorial.ex: -------------------------------------------------------------------------------- 1 | defmodule LeexYeccTutorial do 2 | def lex(s) when is_binary(s), do: s |> to_char_list |> lex 3 | def lex(s) do 4 | {:ok, tokens, _} = :number_lexer.string(s) 5 | tokens 6 | end 7 | 8 | def parse(s) when is_binary(s), do: s |> to_char_list |> parse 9 | def parse(s) do 10 | {:ok, tokens, _} = :number_lexer.string(s) 11 | :number_parser.parse(tokens) |> complete_parse 12 | end 13 | 14 | def complete_parse({:ok, ast}), do: ast 15 | def complete_parse({:error, {line, _, message}}), do: {:error, ["Line: #{line} "] ++ message |> Enum.join} 16 | end 17 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule LeexYeccTutorial.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [app: :leex_yecc_tutorial, 6 | version: "0.0.1", 7 | elixir: "~> 1.2", 8 | build_embedded: Mix.env == :prod, 9 | start_permanent: Mix.env == :prod, 10 | deps: deps] 11 | end 12 | 13 | # Configuration for the OTP application 14 | # 15 | # Type "mix help compile.app" for more information 16 | def application do 17 | [applications: [:logger]] 18 | end 19 | 20 | # Dependencies can be Hex packages: 21 | # 22 | # {:mydep, "~> 0.3.0"} 23 | # 24 | # Or git/path repositories: 25 | # 26 | # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} 27 | # 28 | # Type "mix help deps" for more examples and options 29 | defp deps do 30 | [] 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /src/number_lexer.xrl: -------------------------------------------------------------------------------- 1 | %% src/number_lexer.xrl 2 | 3 | Definitions. 4 | 5 | Whitespace = [\s\t] 6 | Terminator = \n|\r\n|\r 7 | Comma = , 8 | 9 | Digit = [0-9] 10 | NonZeroDigit = [1-9] 11 | NegativeSign = [\-] 12 | Sign = [\+\-] 13 | FractionalPart = \.{Digit}+ 14 | 15 | Bracket = [\[\]] 16 | 17 | IntegerPart = {NegativeSign}?0|{NegativeSign}?{NonZeroDigit}{Digit}* 18 | IntValue = {IntegerPart} 19 | FloatValue = {IntegerPart}{FractionalPart}|{IntegerPart}{ExponentPart}|{IntegerPart}{FractionalPart}{ExponentPart} 20 | 21 | Rules. 22 | 23 | {Comma} : skip_token. 24 | {Whitespace} : skip_token. 25 | {Terminator} : skip_token. 26 | {IntValue} : {token, {int, TokenLine, list_to_integer(TokenChars)}}. 27 | {FloatValue} : {token, {float, TokenLine, list_to_float(TokenChars)}}. 28 | {Bracket} : {token, {list_to_atom(TokenChars), TokenLine, TokenChars} }. 29 | 30 | Erlang code. 31 | -------------------------------------------------------------------------------- /src/number_parser.yrl: -------------------------------------------------------------------------------- 1 | %% src/number_parser.yrl 2 | Nonterminals 3 | document 4 | values value 5 | list list_items. 6 | 7 | Terminals 8 | int float '[' ']'. 9 | 10 | Rootsymbol document. 11 | 12 | 13 | document -> values : '$1'. 14 | 15 | values -> value : ['$1']. 16 | values -> value values : ['$1'] ++ '$2'. 17 | 18 | value -> int : {int, unwrap('$1')}. 19 | value -> float : {float, unwrap('$1')}. 20 | value -> list : '$1'. 21 | 22 | list -> '[' list_items ']' : '$2'. 23 | list_items -> value : ['$1']. 24 | list_items -> value list_items: ['$1'] ++ '$2'. 25 | 26 | 27 | 28 | Erlang code. 29 | 30 | unwrap({_,_,V}) -> V. 31 | -------------------------------------------------------------------------------- /test/leex_yecc_tutorial_test.exs: -------------------------------------------------------------------------------- 1 | defmodule LeexYeccTutorialTest do 2 | use ExUnit.Case 3 | doctest LeexYeccTutorial 4 | 5 | test "the truth" do 6 | assert 1 + 1 == 2 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------