Source

shakespeare-parrot / src / parser / grammar.pg

Full commit
grammar z2::Grammar is PCT::Grammar;

rule TOP {
  <shebang>?
  <title>
  <chardef>*
  <act>*
  {*}
}

rule shebang {
  '#!' \N*
}

rule title {
    <description> <end> 
}
rule scene {
  'scene' <roman> ':' <description> <end>
  <events>*
  {*}
}
rule act {
  'act' <roman> ':' <description> <end>
  {*} #= open
  <scene>* 
  {*} #= close
}

rule events {
  | <line> {*}   #= line
  | <enters> {*} #= enters
  | <exeunts> {*}  #= exeunts
  | <exits> {*}  #= exits
}

rule chardef {
  <character> ',' <description> <end> {*}
}
rule line {
  <character> ':' [<sentence> <end>]+  {*}
}
rule charlist {
  <character>? ['and' <character>]*
}
rule sentence {
  | <tap> {*}  #= tap
  | <plan> {*} #= plan
  | <memorize> {*} #= memorize
  | <branch>        {*} #= branch
  | <question> {*} #= question
  | <test>     {*} #= test
  | <io>          {*} #= io
  | <assignment>  {*} #= assignment
  | <value>       {*} #= value
  | <recall> {*} #= recall
}
token end { <[.!?]> }

rule tap {
  'proove' <value> {*}
}

rule memorize {
  'remember' <value> {*}
}

rule recall {
  'recall' <-end>* {*}
}

rule plan {
  'plan' <value> {*}
}

rule io {
  |'open' <second_person_possessive> 'heart' {*} #= open_heart
  |'speak' <second_person_possessive> 'mind' {*} #= speak_mind
  |'listen' 'to' <second_person_possessive> 'heart' {*} #= listen_heart
  |'open' <second_person_possessive> 'mind' {*} #= open_mind
}

rule assignment {
  |<second_person> <be> 'as' <adjective> 'as' <value> {*}
  |<second_person> <be>? <value> {*}
}

rule branch {
  [ 'let' 'us' | 'we' [ 'shall' | 'must' ] ]  [ 'return' | 'proceed' ] 'to' 'scene' <roman> {*}
}

rule question {
  | <be> <value> <comparison> 'than' <value>{*} #= diseq
  | <be> <value> 'as' <adjective> 'as' <value>{*} #= eq
}

rule comparison {
  | ['more' <negative_adjective> 
    |'less' <positive_adjective>
    |<negative_comparative>] {*} #= worse
  | ['more' <positive_adjective>
    |'less' <negative_adjective> 
    |<positive_comparative>] {*} #= better
}
rule test {
  | 'if so,' <sentence>  {*} #= if
  | 'if not,' <sentence> {*} #= unless
}
rule computation {
  <article>?
  [
  |<binary> {*} #= binary
  |<unary> {*} #= unary
  ]
}

rule binary {
    |'remainder' 'of' 'the' 'quotient' 'between' <value> 'and' <value> {*} #= mod
    |'difference' 'between' <value> 'and' <value> {*} #= sub
    |'sum' 'of' <value> 'and' <value> {*} #= add
    |'product' 'of' <value> 'and' <value> {*} #= mul
    |'quotient' 'between' <value> 'and' <value> {*} #= div
}

rule unary {
    |'factorial' 'of' <value> {*} #= fact
    |'square' 'of' <value> {*} #= square
    |'square root' 'of' <value> {*} #= sqrt
    |'cube' 'of' <value> {*} #= cube
    |'twice' <value> {*} #= dup
}

rule value {
  |<reference> {*}   #= reference
  |<immediate> {*}   #= immediate
  |<computation> {*} #= computation
}

rule immediate {
  <article>? [<adjective> ]* <noun> {*} 
}

token noun {
  |<positive_noun>
  |<negative_noun>
  |<neutral_noun> 
  |<nothing>
}

token reference {
  |<second_person_reflexive> {*} #= you_ref
  |<second_person> {*} #= you_ref
  |<first_person> {*} #= self_ref
  |<first_person_reflexive> {*} #= self_ref
  |<character> {*} #= named_ref
}

token adjective {
  |<positive_adjective>
  |<neutral_adjective>
  |<negative_adjective>
  |<first_person_possessive>
  |<second_person_possessive>
  |<third_person_possessive>
}

token roman {
  ['iii' | 'ii' |  'iv' | 'i' | 'vii' | 'vi' | 'v' ]
}

rule enters {
  '[' 
    'enter' <charlist> {*} 
  ']'
}
rule exeunts {
  '[' 
   'exeunt' <charlist> {*} 
  ']'
}

rule exits {
 '[' 
  'exit' <character> {*} 
 ']'
}
token description {
  <-end>* 
}