Ruby BNF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

BNF Syntax of Ruby

Based on http://docs.huihoo.com/ruby/ruby-man-1.4/yacc.html with some editing. I have found other websites attesting that Ruby version 1.4.6 is the latest with a reference manual in English, http://docs.huihoo.com/ruby/ruby-man-1.4/index.html, though Ruby exists up to v1.9 in Japanese. ALL-CAPS are used for nonterminals, and all-lowercase for literal keywords. Literal ( ) [ ] { } are quoted to distinguish them from BNF syntax. PROGRAM T COMPSTMT STMT : COMPSTMT : ";" | "\n" //a newline can terminate a statement

ARG

: STMT {T EXPR} [T] : | | | | | | | | | | : | | | | | | | | CALL do ["|" [BLOCK_VAR] "|"] COMPSTMT end undef FNAME alias FNAME FNAME STMT if EXPR STMT while EXPR STMT unless EXPR STMT until EXPR "BEGIN" "{" COMPSTMT "}" //object initializer "END" "{" COMPSTMT "}" //object finalizer LHS = COMMAND [do ["|" [BLOCK_VAR] "|"] COMPSTMT end] EXPR MLHS = MRHS return CALL_ARGS yield CALL_ARGS EXPR and EXPR EXPR or EXPR not EXPR COMMAND ! COMMAND ARG

: | | | | | | | | | | | | | | | |

LHS = ARG LHS OP_ASGN ARG ARG .. ARG | ARG ... ARG ARG + ARG | ARG - ARG | ARG * ARG | ARG / ARG ARG % ARG | ARG ** ARG + ARG | - ARG ARG "|" ARG ARG ARG | ARG & ARG ARG <=> ARG ARG > ARG | ARG >= ARG | ARG < ARG | ARG <= ARG ARG == ARG | ARG === ARG | ARG != ARG ARG = ARG | ARG ! ARG ! ARG | ARG ARG << ARG | ARG >> ARG ARG && ARG | ARG || ARG defined? ARG PRIMARY COMPSTMT ")" LITERAL VARIABLE PRIMARY :: IDENTIFIER :: IDENTIFIER PRIMARY "[" [ARGS] "]" "[" [ARGS [,]] "]" "{" [ARGS | ASSOCS [,]] "}" return ["(" [CALL_ARGS] ")"] yield ["(" [CALL_ARGS] ")"] defined? "(" ARG ")" FUNCTION FUNCTION "{" ["|" [BLOCK_VAR] "|"] COMPSTMT "}" if EXPR THEN COMPSTMT {elsif EXPR THEN COMPSTMT} [else COMPSTMT] end unless EXPR THEN COMPSTMT [else COMPSTMT] end while EXPR DO COMPSTMT end until EXPR DO COMPSTMT end case COMPSTMT when WHEN_ARGS THEN COMPSTMT {when WHEN_ARGS THEN COMPSTMT} [else COMPSTMT] end

EXPR

PRIMARY: "(" | | | | | | | | | | | | |

CALL

: FUNCTION | COMMAND : | | | : | | | | | | OPERATION CALL_ARGS PRIMARY.OPERATION CALL_ARGS PRIMARY :: OPERATION CALL_ARGS super CALL_ARGS OPERATION ["(" [CALL_ARGS] ")"] PRIMARY.OPERATION "(" [CALL_ARGS] ")" PRIMARY :: OPERATION "(" [CALL_ARGS] ")" PRIMARY.OPERATION PRIMARY :: OPERATION super "(" [CALL_ARGS] ")" super |

COMMAND

FUNCTION

| | |

| for BLOCK_VAR in EXPR DO COMPSTMT end | begin COMPSTMT {rescue [ARGS] DO COMPSTMT} [else COMPSTMT] [ensure COMPSTMT] end | class IDENTIFIER [< IDENTIFIER] COMPSTMT end | module IDENTIFIER COMPSTMT end | def FNAME ARGDECL COMPSTMT end | def SINGLETON (. | ::) FNAME ARGDECL COMPSTMT end WHEN_ARGS THEN DO BLOCK_VAR MLHS : ARGS [, * ARG] | * ARG

ARGDECL

: "(" ARGLIST ")" | ARGLIST T : IDENTIFIER(,IDENTIFIER)*[, *[IDENTIFIER]][,&IDENTIFIER] | *IDENTIFIER[, &IDENTIFIER] | [&IDENTIFIER] : VARIABLE | "(" EXPR ")" : ASSOC {, ASSOC} : ARG => ARG : VARNAME | nil | self : numeric | SYMBOL | STRING | STRING2 | HERE_DOC | REGEXP

ARGLIST

SINGLETON

ASSOCS ASSOC VARIABLE LITERAL

The following are recognized by the lexical analyzer. OP_ASGN : += | -= | *= | /= | %= | **= | &= | |= | = | <<= | >>= | &&= | ||= : :FNAME | :VARNAME : IDENTIFIER | .. | "|" | | & | <=> | == | === | = | > | >= | < | <= | + | - | * | / | % | ** | << | >> | | +@ | -@ | [] | []= : IDENTIFIER [! | ?] : GLOBAL | @IDENTIFIER | IDENTIFIER : $IDENTIFIER | $any_char | $-any_char : " {any_char} " | {any_char} | {any_char} : %(Q|q|x)char {any_char} char : <<(IDENTIFIER | STRING) {any_char} IDENTIFIER : / {any_char} / [i|o|p] | %r char {any_char} char /[a-zA-Z_]{a-zA-Z0-9_}/.

SYMBOL FNAME : T | then | T then : T | do | T do : LHS | MLHS : MLHS_ITEM , [MLHS_ITEM (, MLHS_ITEM)*] [* [LHS]] | * LHS : LHS | "(" MLHS ")" STRING LHS : VARIABLE | PRIMARY "[" [ARGS] "]" | PRIMARY.IDENTIFIER STRING2 MRHS CALL_ARGS : ARGS [, * ARG] : | | | | | * ARG HERE_DOC ARGS ARGS [, ASSOCS] [, * ARG] [, & ARG] ASSOCS [, * ARG] [, & ARG] * ARG [, & ARG] | & ARG COMMAND //"then" and "do" can go on next line

OPERATION VARNAME GLOBAL

MLHS_ITEM

REGEXP

ARGS

: ARG (, ARG)*

IDENTIFIER : sequence in

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy