Skip to content

Instantly share code, notes, and snippets.

@aaronranard aaronranard/ast.md
Last active Sep 1, 2017

Embed
What would you like to do?
abstract syntax tree

Syntax

BNF grammar is in: examples/lang.bnf Lex grammar is in: examples/lang.lex

To see what is all possible view examples/test.lang

Run

./bin/syntax \
    --grammar examples/lang.bnf \
    --lex examples/lang.lex \
    --mode lalr1 \
    --file examples/test.lang

./bin/syntax --lex examples/lang.lex --tokenize -p '2 + 5'

List of tokens:

 [
  {
    "type": "NUMBER",
    "value": "2",
    "startOffset": 0,
    "endOffset": 1,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 0,
    "endColumn": 1
  },
  {
    "type": "ADDITIVE_OPERATOR",
    "value": "+",
    "startOffset": 2,
    "endOffset": 3,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 2,
    "endColumn": 3
  },
  {
    "type": "NUMBER",
    "value": "5",
    "startOffset": 4,
    "endOffset": 5,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 4,
    "endColumn": 5
  }
] 

./bin/syntax --lex examples/lang.lex --tokenize -p 'var[0]'

List of tokens:

 [
  {
    "type": "IDENTIFIER",
    "value": "var",
    "startOffset": 0,
    "endOffset": 3,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 0,
    "endColumn": 3
  },
  {
    "type": "LBRACKET",
    "value": "[",
    "startOffset": 3,
    "endOffset": 4,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 3,
    "endColumn": 4
  },
  {
    "type": "NUMBER",
    "value": "0",
    "startOffset": 4,
    "endOffset": 5,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 4,
    "endColumn": 5
  },
  {
    "type": "RBRACKET",
    "value": "]",
    "startOffset": 5,
    "endOffset": 6,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 5,
    "endColumn": 6
  }
] 

./bin/syntax --lex examples/lang.lex --tokenize -p 'var = 0'

List of tokens:

 [
  {
    "type": "IDENTIFIER",
    "value": "var",
    "startOffset": 0,
    "endOffset": 3,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 0,
    "endColumn": 3
  },
  {
    "type": "SIMPLE_ASSIGN",
    "value": "=",
    "startOffset": 4,
    "endOffset": 5,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 4,
    "endColumn": 5
  },
  {
    "type": "NUMBER",
    "value": "0",
    "startOffset": 6,
    "endOffset": 7,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 6,
    "endColumn": 7
  }
] 

./bin/syntax --lex examples/lang.lex --tokenize -p 'var == 0'

List of tokens:

 [
  {
    "type": "IDENTIFIER",
    "value": "var",
    "startOffset": 0,
    "endOffset": 3,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 0,
    "endColumn": 3
  },
  {
    "type": "EQUALITY_OPERATOR",
    "value": "==",
    "startOffset": 4,
    "endOffset": 6,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 4,
    "endColumn": 6
  },
  {
    "type": "NUMBER",
    "value": "0",
    "startOffset": 7,
    "endOffset": 8,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 7,
    "endColumn": 8
  }
] 

./bin/syntax --lex examples/lang.lex --tokenize -p ' -200 < weight < 2000'

List of tokens:

 [
  {
    "type": "ADDITIVE_OPERATOR",
    "value": "-",
    "startOffset": 1,
    "endOffset": 2,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 1,
    "endColumn": 2
  },
  {
    "type": "NUMBER",
    "value": "200",
    "startOffset": 2,
    "endOffset": 5,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 2,
    "endColumn": 5
  },
  {
    "type": "RELATIONAL_OPERATOR",
    "value": "<",
    "startOffset": 6,
    "endOffset": 7,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 6,
    "endColumn": 7
  },
  {
    "type": "IDENTIFIER",
    "value": "weight",
    "startOffset": 8,
    "endOffset": 14,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 8,
    "endColumn": 14
  },
  {
    "type": "RELATIONAL_OPERATOR",
    "value": "<",
    "startOffset": 15,
    "endOffset": 16,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 15,
    "endColumn": 16
  },
  {
    "type": "NUMBER",
    "value": "2000",
    "startOffset": 17,
    "endOffset": 21,
    "startLine": 1,
    "endLine": 1,
    "startColumn": 17,
    "endColumn": 21
  }
] 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.