Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

grammar parsers

Name: Anonymous 2010-11-30 17:46

I want to made a simple programming language, what should I use for parsing:

1. lex + yacc
2. lemon
3. parsec
4. other (what?)

Name: 2010-11-30 17:47

Name: Anonymous 2010-11-30 17:48

UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE  UNITE

Name: Anonymous 2010-12-01 16:28

ragel

Name: Anonymous 2010-12-01 16:54

lex/yacc variants are usually fine, but depend on your grammer. For certain grammars, you might want to write the parser manually.
Why do you even want syntax?

Name: Anonymous 2010-12-01 17:13

>>1
If you want to make a simple language, you're doing it wrong if you require a parser generator. A simple language should be a Lisp or a concatenative language like Forth, or something equally simple. If your language is so complex that you can't write its parser by hand, code in it it will be too complicated to generate as well.

Name: Anonymous 2010-12-01 17:14

>>6
APL syntax is also very simple. I knew there was at least one more.

Name: Anonymous 2010-12-01 18:31

If you need external lexer/parser, then your language is shit.

digit x -> x.{\0=>0;\1=>1;\2=>2;\3=>3;\4=>4;\5=>5;\6=>6;\7=>7;\8=>8;\9=>9}
number [@xs] -> {r [] => r; r [(x:!digit)@xs] => rec 10r+x xs} 0 xs
op [x] -> x.{\+ => '`+`; \- => '`-`; \* => '`*`; \/ => '`/`}
term x -> x.{[\( @(a:!expr) \)] => [a]; [@(a:!number)] => a}
expr x -> x.{[@(a:!term)] => a; [@(a:!term) @(o:!op) @(b:!expr)] => [o a b]}
strip p l -> l.{x=>x; [@xs !p @ys]=>[@xs @(rec ys)]}
parse string -> string.asList.(strip \Space ?).expr

Name: Anonymous 2010-12-01 18:34

>>8
that is without a doubt the ugliest code i've ever seen.
what language is that?

Name: Anonymous 2010-12-01 18:40

>>9
>what language is that?
Lisp DSL

>that is without a doubt the ugliest code i've ever seen.
But this code does:
1. lexing of numbers
2. lexing of operators
3. parsing them into infix expression with braces
4. its output can be passed directly to "eval"
5. no external ultilities used (even "strip" defined)

Name: Anonymous 2010-12-01 18:55

>>10
It sure looks pretty non-Lispy, but that's acceptable for more extreme DSLs, is it some published piece of code or something you wrote but didn't publish?

Name: Anonymous 2010-12-01 18:57

Binary files can also be parsed just as text ones. For example..

Lsb2 -> 0
parseLsb2 [a b] -> b<<8 + a
breakLsb2 x -> [mod,x,256  mod,x>>8,256]
setSizeof Lsb2 2

Lsb4 -> 0
parseLsb4 [a b c d] -> d<<24 + c<<16 + b<<8 + a
breakLsb4 x -> [mod,x    ,256  mod,x>> 8,256
                mod,x>>16,256  mod,x>>24,256]
setSizeof Lsb4 4

ElfId -> []
parseElfId x -> x when x.len==16
breakElfId x -> x
setSizeof ElfId 16

ElfType -> gElfTypes|0
parseElfType x -> if v:x.parseLsb2 (fnd {[!v _ _]} gElfTypes) || #($x UNKNOWN "Unknown")
breakElfType [x _ _] -> x.breakLsb
setSizeof ElfType 2

machineInfo x -> (fnd [id _ _]~>id==x gMachines) ||
                 #(RESERVED $x UNKNOWN "Unknown")

ElfMachineId -> gMachines|0
parseElfMachineId x -> v.machineInfo when v:x.parseLsb2
breakElfMachineId [x _ _] -> x.breakLsb2
setSizeof ElfMachineId 2


class ElfHeader
  elfId     = ElfId // processor independent header part
  type      = ElfType
  machine   = ElfMachineId
  version   = Lsb4 // file format version
  entry     = Lsb4 // entry into program or null, if file has no entry point
  phoff     = Lsb4 // program header table offset
  shoff     = Lsb4 // section header table offset
  flags     = Lsb4
  ehsize    = Lsb2 // size of this ElfHeader
  phentsize = Lsb2 // program header table: entry size
  phnum     = Lsb2 // program header table: number of entries
  shentsize = Lsb2 // section header table: entry size
  shnum     = Lsb2 // section header table: number of entries
  shstrndx  = Lsb2 // string table section index or SHN_UNDEF


class Elf header segments sections strings data

loadElfHeader file -> file.fget.asList |>
  {     default        => error "not an ELF: $file"
   f:[127 69 76 70 @_] => f} |>
  {data:[@(hdr:!parseElfHeader) @rest] => (Elf header=hdr data=data)}


gMachines =: (ltl '{
//Id  Symbol         Description
   0  EM_NONE        "No machine"
   1  EM_M32         "AT&T WE 32100"
   2  EM_SPARC       "SPARC"
   3  EM_386         "Intel 80386"
   4  EM_68K         "Motorola 68000"
   5  EM_88K         "Motorola 88000"
   7  EM_860         "Intel 80860"
   8  EM_MIPS        "MIPS I Architecture"
  10  EM_MIPS_RS3_LE "MIPS RS3000 Little-endian"
  15  EM_PARISC      "Hewlett-Packard PA-RISC"
  17  EM_VPP500      "Fujitsu VPP500"
  18  EM_SPARC32PLUS "Enhanced instruction set SPARC"
  19  EM_960         "Intel 80960"
  20  EM_PPC         "Power PC"
  36  EM_V800        "NEC V800"
  37  EM_FR20        "Fujitsu FR20"
  38  EM_RH32        "TRW RH-32"
  39  EM_RCE         "Motorola RCE"
  40  EM_ARM         "Advanced RISC Machines ARM"
  41  EM_ALPHA       "Digital Alpha"
  42  EM_SH          "Hitachi SH"
  43  EM_SPARCV9     "SPARC Version 9"
  44  EM_TRICORE     "Siemens Tricore embedded processor"
  45  EM_ARC         "Argonaut RISC Core, Argonaut Technologies Inc."
  46  EM_H8_300      "Hitachi H8/300"
  47  EM_H8_300H     "Hitachi H8/300H"
  48  EM_H8S         "Hitachi H8S"
  49  EM_H8_500      "Hitachi H8/500"
  50  EM_IA_64       "Intel MercedTM Processor"
  51  EM_MIPS_X      "Stanford MIPS-X"
  52  EM_COLDFIRE    "Motorola Coldfire"
  53  EM_68HC12      "Motorola M68HC12"
  })

Name: Anonymous 2010-12-01 19:05

>>11
No. I didn't published it yet, but it is intended as a scripting language on top of Common Lisp (just as Lua for C/C++), so I can use SBCL as a replacement for BASH. Some time ago I wanted to do Lisp-centered Linux distro, maybe I will return to this idea and release it with this language.

Name: Anonymous 2010-12-01 20:09

>>13
No. I didn't published it yet

Good thing too, as you need to parse your own grammar first.

Name: Anonymous 2010-12-01 20:21

>>14
LOL. I dont have any grammar. To parse my DSL I use simple operator folding. This way I can have full power of Lisp, without sacrificing syntatic macros and reader macros.

Here is an example of simple macro, that implements "dotimes" like functionality

m:times head @body ->
  $@(do c:head.{_=>gensym; #(`:` $c $n)=>c}
        n:head.{x=>x     ; #(`:` $c $n)=>n}
        #(&e:$n {$c => if $c!=&e do $body rec,$c+1} 0))

// Usage: times i:10 say i

With traditional parsers this wouldn't be possible.

Name: Anonymous 2010-12-02 0:28

>>15
I dont have any grammar.
Is this like those people who don't speak any languages?

Name: ooog the caveman 2010-12-02 0:50

>>16
even ooog speak using grammar in cave talk

Name: Anonymous 2010-12-02 16:51

>>13
scripting language on top of Common Lisp
That makes a lot more sense when the host language is unusable, like C or C++. If you find Common Lisp clunky as a command line, why don't you just write a less drastic library for the capabilities you need?

Name: Anonymous 2010-12-02 17:21

>>18
Why crawl when you can fly? I want the most advanced and easy to use language that is possible in our universe! ^_^

Name: Anonymous 2010-12-02 22:15

Coco/r, ANTLR, manual rdp.

Name: Anonymous 2011-01-31 20:30

<-- check em dubz

Name: Anonymous 2011-01-31 20:37

parser combinators ftw, lex + yacc is ugly!

Name: Anonymous 2011-02-03 5:11

Name: Anonymous 2011-02-04 16:29

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List