FreeBASIC grammar
Grammar Notation
Format of a production
left hand side: right hand side;
: should be read as 'is defined as'.
The right hand side of a production is terminated by a ;.
A word in italics represent the name of a production (the left
hand side of the production).
Few operators are used to describe the FreeBASIC grammar.
Any symbol that appears on the right hand side of a production
that is not an operator and does not appear in italics
represents itself and appears bold.
A symbol at the right hand side of a rule can refer to a production.
Such references are in italics.
For navigational purposes a reference is a link to the production
being referenced.
When reading the grammar be aware that FreeBASIC is a case insensitive
language.
The grammar presented is not an exact statement of the FreeBASIC language.
Go straight to:
program
expression
Tokens
white: \t |
any_char: any valid character;
eol: \n|\r|\n\r;
statement_separator: ( : | eol )+;
dot: .;
sign: +|-;
alpha: a|b|c|d|e|f|g|h|i|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z;
digit: 0|1|2|3|4|5|6|7|8|9;
hexdigit: a|b|c|d|e|f|digit;
octdigit: 0|1|2|3|4|5|6|7;
bindigit: 0|1;
alphadigit: alpha| digit;
integer_suffix: %|&|l|u|ul|ll|ull;
floating_point_suffix: !|#|f;
suffix: integer_suffix|floating_point_suffix|$;
expchar: d|e;
operator
literal
integer_literal
decimal_integer: digit+;
hexadecimal integer: &hhexdigit+;
octal_integer: &ooctdigit+;
binary_integer: &bbindigit+;
floating_literal
string_literal
escape_sequence
decimal_escape_sequence
hexadecimal_escape_sequence
octal_escape_sequence
binary_escape_sequence
Comment
comment
Toplevel
program
line
label
statement
(declaration | procedure_call_or_assign | compound_statement | quirk_statement | assembler_block | assignment )?
(statement_separator statement)*
compound_statement
namespace_statement
scope_statement: scope statement_separator statement* end scope
short_if_statement
else statement_separator statement*
(eol| end if | endif)
statement*
elseif_block*
(else statement_separator statement*)?
(end if|endif)
for_statement
(statement|exit for(, for)* | continue for (, for)*)* next identifier (, identifier)* statement_separator
(statement | exit while (, while)* | continue while (, while)*)*
wend
case_expression
assembler_block
assignment
variable
const_declaration
type_or_union_declaration
type_declaration
variable_declaration
symbol_type
parameter
user_defined_type
procedure_declaration
procedure_parameter
expressions
expression
boolean_expression
logical_expression
logical_or_expression
logical_and_expression
relational_expression
concatenation_expression
add_expression
;
shift_expressionFormat of a production
left hand side: right hand side;
: should be read as 'is defined as'.
The right hand side of a production is terminated by a ;.
A word in italics represent the name of a production (the left
hand side of the production).
Few operators are used to describe the FreeBASIC grammar.
operator | meaning |
. | any character |
* | 0 or more (repetition) |
+ | 1 or more (repetition) |
? | optional (choice) |
() | grouping |
| | separator (separates alternatives) |
semicolon | end of production |
Any symbol that appears on the right hand side of a production
that is not an operator and does not appear in italics
represents itself and appears bold.
A symbol at the right hand side of a rule can refer to a production.
Such references are in italics.
For navigational purposes a reference is a link to the production
being referenced.
When reading the grammar be aware that FreeBASIC is a case insensitive
language.
The grammar presented is not an exact statement of the FreeBASIC language.
Go straight to:
program
expression
Tokens
white: \t |
any_char: any valid character;
eol: \n|\r|\n\r;
statement_separator: ( : | eol )+;
dot: .;
sign: +|-;
alpha: a|b|c|d|e|f|g|h|i|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z;
digit: 0|1|2|3|4|5|6|7|8|9;
hexdigit: a|b|c|d|e|f|digit;
octdigit: 0|1|2|3|4|5|6|7;
bindigit: 0|1;
alphadigit: alpha| digit;
integer_suffix: %|&|l|u|ul|ll|ull;
floating_point_suffix: !|#|f;
suffix: integer_suffix|floating_point_suffix|$;
expchar: d|e;
operator
: = | < | > |<> |+ | - | * | @
binary_operator & | -> | / | \ | ^ | andalso
orelse | and | or | xor | eqv | imp
+= | -= | *= | /= | \= | ^= | &= |
and= | or= | xor= | eqv= | imp=
new | delete | delete[] | cast | procptr
varptr | strptr | sizeof | [] | ()
;orelse | and | or | xor | eqv | imp
+= | -= | *= | /= | \= | ^= | &= |
and= | or= | xor= | eqv= | imp=
new | delete | delete[] | cast | procptr
varptr | strptr | sizeof | [] | ()
: = | < | > |<> |+ | - |
identifier & | -> | / | \ | ^
+= | -= | *= | /= | \= | ^= | &= |
and= | or= | xor= | eqv= | imp=
andalso | orelse
;+= | -= | *= | /= | \= | ^= | &= |
and= | or= | xor= | eqv= | imp=
andalso | orelse
literal
integer_literal
decimal_integer: digit+;
hexadecimal integer: &hhexdigit+;
octal_integer: &ooctdigit+;
binary_integer: &bbindigit+;
floating_literal
string_literal
escape_sequence
: simple_escape_sequence
simple_escape_sequence unicode_escape_sequence
decimal_escape_sequence
hexadecimal_escape_sequence
octal_escape_sequence
binary_escape_sequence
;decimal_escape_sequence
hexadecimal_escape_sequence
octal_escape_sequence
binary_escape_sequence
: \a|\b|\f|\l|\n|\r|\t|\v|\\|\'|\"
;
unicode_escape_sequence;
decimal_escape_sequence
hexadecimal_escape_sequence
octal_escape_sequence
binary_escape_sequence
Comment
comment
: ( ' | rem) (($directive) | (any_char_but_eol*))
;
multiline_nested_comment;
Toplevel
program
line
label
statement
(declaration | procedure_call_or_assign | compound_statement | quirk_statement | assembler_block | assignment )?
(statement_separator statement)*
;
declaration:(public|private)?
procedure_call_or_assign(
declare procedure_declaration
;(static
function_definition
sub_definition
destructor_definition
property_definition
constructor_definition
operator_definition
const_declaration
type_or_union_declaration
variable_declaration
enumeration_declaration
auto_variable_declaration
)( function_definition
) sub_definition
operator_definition
constructor_definition
destructor_definition
property_definition
variable_declaration
)operator_definition
constructor_definition
destructor_definition
property_definition
variable_declaration
function_definition
sub_definition
destructor_definition
property_definition
constructor_definition
operator_definition
const_declaration
type_or_union_declaration
variable_declaration
enumeration_declaration
auto_variable_declaration
declare procedure_declaration
compound_statement
namespace_statement
scope_statement: scope statement_separator statement* end scope
;
if_statementshort_if_statement
else statement_separator statement*
(eol| end if | endif)
;
long_if_statementstatement*
elseif_block*
(else statement_separator statement*)?
(end if|endif)
;
elseif_blockfor_statement
(statement|exit for(, for)* | continue for (, for)*)* next identifier (, identifier)* statement_separator
;
do_statement: do (until|while) expression (statement|exit do (, do)* | continue do (, do)*)* loop
while_statement do (statement|exit do (, do)* | continue do (, do)*)* loop (until|while) expression statement_separator
; (statement | exit while (, while)* | continue while (, while)*)*
wend
;
select_statement: select case (as const) expression case_statement* case else statement_separator statement* end select
;
case_statement;
case_expression
assembler_block
assignment
variable
const_declaration
type_or_union_declaration
type_declaration
: type identifier (alias string_literal)? (field = expression)? (comment)? statement_separator
end type
;
union_declarationend type
;
: union identifier (alias string_literal)? (field = expression)? (comment)? statement_separator
;
type_member_declarationunion_member_declaration+
end union;
variable_declaration
symbol_type
: const? unsigned?
(
scalar
;
scalar(
scalar
string (* integer_literal)?
wstring (* integer_literal)?
user_defined_type
function (( parameters )) (as symbol_type)
sub (( parameters ))
)(const? (ptr|pointer))*wstring (* integer_literal)?
user_defined_type
function (( parameters )) (as symbol_type)
sub (( parameters ))
;
: byte
parameters ubyte
short
ushort
integer
uinteger
longint
ulongint
long
ulong
single
double
;short
ushort
integer
uinteger
longint
ulongint
long
ulong
single
double
parameter
user_defined_type
procedure_declaration
: static? (sub_declaration|function_declaration|constructor_declaration|destructor_declaration|operator_declaration)
;
procedure_parameter_list;
procedure_parameter
expressions
expression
boolean_expression
logical_expression
logical_or_expression
logical_and_expression
relational_expression
concatenation_expression
add_expression
:
shift_expression( ( + | - ) shift_expression )*;
:
mod_expression ( (shl | shr) mod_expression )*;
: =
integer_division_expression( mod integer_division_expression )*;
multiplication_expression
exponentiation_expression
prefix_expression
highest_precedence_expression
: address_of_expression
address_of_expression ( dereference_expression | casting_expression | pointer_type_casting_expression | parenthesised_expression )
anonymous_udt
atom
;anonymous_udt
atom
: varptr ( highest_precedence_expression )
dereference_expression procptr ( identifier (())? )
@ (identifier (())? | highest_precedence_expression)
sadd|strptr ( expression )
;@ (identifier (())? | highest_precedence_expression)
sadd|strptr ( expression )
casting_expression
quirk_function
quirk_function_name
: mkd | mki | mkl | mklongint | mkshort
quirk_statement cvd | cvi | cvl | cvlongint | cvs | cvshort
asc | chr | instr | instrev | lcase | left | len | lset | ltrim | mid | right | rset | rtrim | space | string | ucase | wchr | wstr | wstring
abs | sgn | fix | frac | len | sizeof,sin | asin | cos | acos | tan | atn | sqr | log | exp | atan2 | int
peek
lbound | ubound
seek | input | open | close | get | put | name
err
iif
va_first
cbyte | cshort | cint | clng | clngint | cubyte | cushort | cuint | culng | culngint | csng | cdbl | csign | cunsg
type
view | width | color | screen
;asc | chr | instr | instrev | lcase | left | len | lset | ltrim | mid | right | rset | rtrim | space | string | ucase | wchr | wstr | wstring
abs | sgn | fix | frac | len | sizeof,sin | asin | cos | acos | tan | atn | sqr | log | exp | atan2 | int
peek
lbound | ubound
seek | input | open | close | get | put | name
err
iif
va_first
cbyte | cshort | cint | clng | clngint | cubyte | cushort | cuint | culng | culngint | csng | cdbl | csign | cunsg
type
view | width | color | screen
: jump_statement
jump_statement print_statement
data_statement
array_statement
line_input_statement
input_statement
poke_statement
file_statement
write_statement
error_statement
on_statement
view_statement
mid_statement
lrset_statement
width_statement
color_statement
gfx_statement
;data_statement
array_statement
line_input_statement
input_statement
poke_statement
file_statement
write_statement
error_statement
on_statement
view_statement
mid_statement
lrset_statement
width_statement
color_statement
gfx_statement
print_statement
data_statement
array_statement
line_input_statement
input_statement
poke_statement
file_statement
: close (#? expression) (, #? expression)*
write_statement seek #? expression , expression
put # expression , expression? , expression
get # expression , expression? , variable
(lock|unlock) #? expression , expression (to expression)?
name expression as expression
;put # expression , expression? , expression
get # expression , expression? , variable
(lock|unlock) #? expression , expression (to expression)?
name expression as expression
error_statement
on_statement
view_statement
mid_statement
lrset_statement
width_statement
color_statement
gfx_statement
: pset ( expression , )? step? ( expression , expression ) (, expression )?
line ( expression , )? step? (( expression , expression ) )? - step? ( expression , expression ) (, expression? (, string_literal? (, expression )?)?)?
circle ( expression , )? step? ( expression , expression ) , expression ((, expression? (, expression? (, expression? (, expression (, expression)? )? )?)?)?)?
paint ( expression , )? step? ( expression , expression ) (, expression? (, expression? ) )
draw ( expression , )? expression
view (screen? ( expression , expression ) - ( expression , expression ) (, expression? (, expression)?)? )?
palette get? ((using variable) | (expression , expression (, expression , expression)?)?)
put ( expression , )? step? ( expression , expression ) , ( ( expression , expression ) - ( expression , expression ) ,)? variable (, expression (, expression)?)?
get ( expression , )? step? ( expression , expression ) - step? ( expression , expression ) , variable
screen (integer_literal | ((expression (((, expression)? , expression)? expression)? , expression))
screenres expression , expression (((, expression)? , expression)? , expression)?
;circle ( expression , )? step? ( expression , expression ) , expression ((, expression? (, expression? (, expression? (, expression (, expression)? )? )?)?)?)?
paint ( expression , )? step? ( expression , expression ) (, expression? (, expression? ) )
draw ( expression , )? expression
view (screen? ( expression , expression ) - ( expression , expression ) (, expression? (, expression)?)? )?
palette get? ((using variable) | (expression , expression (, expression , expression)?)?)
put ( expression , )? step? ( expression , expression ) , ( ( expression , expression ) - ( expression , expression ) ,)? variable (, expression (, expression)?)?
get ( expression , )? step? ( expression , expression ) - step? ( expression , expression ) , variable
screen (integer_literal | ((expression (((, expression)? , expression)? expression)? , expression))
screenres expression , expression (((, expression)? , expression)? , expression)?