| Home / Indice sezione | www.icosaedro.it | ![]() |
|
| ||
| Frontespizio Introduzione Caratteristiche Un semplice esempio Moduli Sezione IMPORT Sezione CONST Sezione TYPE Tipo FORWARD Sezione VAR Sezione FUNCTION Sezione BEGIN Stringhe letterali Sottostringhe Commenti Costanti predefinite Variabili predefinite Funzioni predefinite Istruzione di assegnamento Istruzione per la chiamata di funzione Ordine di valutazione degli argomenti attuali Istruzione IF Istruzione SWITCH Istruzione FOR Istruzione WHILE Istruzione REPEAT Istruzione LOOP Istruzione TRY Istruzione RAISE ERROR Istruzione RETURN Espressioni logiche Espressioni intere Espressioni reali Espressioni stringa Espressioni tra tipi strutturati Regole di scope M2 per programmatori C M2 per programmatori Java Rappresentazione dei dati in memoria Keywords Messaggi di errore a runtime Sintassi | SintassiIl formalismo BNF permette di descrivere sinteticamente la sintassi del linguaggio M2. Il sorgente del programma deve corrispondere ad una compilation_unit. Il rispetto delle clausole sintattiche è condizione necessaria ma non sufficiente perché la compilazione si concluda correttamente. Vari aspetti della sintassi e della semantica del linguaggio sono discussi nel testo. Tra due elementi sintattici si possono inserire un numero arbitrario di spazi, caratteri di tabulazione, a-capo e commenti. Questi caratteri vengono ignorati dal compilatore e non contribuiscono al codice prodotto. Fanno eccezione le stringhe letterali (string), i numeri (number) e i nomi qualificati (qualified_name): questi elementi sintattici non possono essere interrotti da spaziature o altri caratteri. Tutte le dichiarazioni BNF sono numerate, e i numeri a pedice degli identificatori permettono di trovare rapidamente la corrispondente dichiarazione. 1. compilation_unit = definition_module2 | implementation_module3 | module4 ;2. definition_module = "DEFINITION" "MODULE" module_name5 import6 { const_decl7 | var_decl11 | function_decl23 } "END" ;3. implementation_module = "IMPLEMENTATION" "MODULE" module_name5 import6 { const_decl7 | type_decl10 | var_decl11 | function_decl23 function_body25 } "END" ;4. module = "MODULE" module_name5 import6 function_body25 ;5. module_name = name68 ; 6. import = { "IMPORT" [ module_name5 { "," module_name5 } ] } ;7. const_decl = "CONST" { name68 "=" const_value8 } ;8. const_value = boolean48 | [ "+" | "-" ] number49 | [ "+" | "-" ] const_name9 | string60 ;9. const_name = qualified_name70 ; 10. type_decl = "TYPE" { name68 "=" ( "FORWARD" | type12 ) } ;11. var_decl = "VAR" [ "STATIC" ] { name68 { "," name68 } ":" type12 } ;12. type = simple_type14 | array_type18 | record_type19 | function_type22 | type_name13 ; 13. type_name = qualified_name70 ; 14. simple_type = "VOID" | "BOOLEAN" | "INTEGER" | enum_type15 | "REAL" | "STRING" ;15. enum_type = "(" enum_elem16 { "," enum_elem16 } ")" ;16. enum_elem = name68 [ "=" const_integer17 ] ;17. const_integer = int_number52 | const_name9 ; 18. array_type = "ARRAY" "OF" type12 ;19. record_type = "RECORD" { field_decl20 } "END" ;20. field_decl = field_name21 { "," field_name21 } ":" type12 ;21. field_name = name68 ; 22. function_type = function_decl23 ; 23. function_decl = "FUNCTION" name68 "(" [ formal_arg24 { "," formal_arg24 } ] ")" [ ":" type12 ] [ "RAISE" "ERROR" ] ;24. formal_arg = [ "VAR" ] name68 ":" type12 ;25. function_body = { const_decl7 | type_decl10 | var_decl11 | function_decl23 function_body25 } "BEGIN" { instruction26 } "END" ;26. instruction = assignment27 | function_call28 | if33 | switch34 | while35 | repeat36 | for37 | loop38 | exit39 | try40 | raise41 | return42 ; 27. assignment = var_name64 { selector65 } "=" expr43 ;28. function_call = prefix_function_call29 | postfix_function_call30 ; 29. prefix_function_call = function_name31 "(" { actual_arg32 } ")" ;30. postfix_function_call = ( var_name64 | function_name31 "(" { actual_arg32 } ")" ) { "->" qualified_name70 "(" { actual_arg32 } ")" } ;31. function_name = qualified_name70 ; 32. actual_arg = expr43 | var_name64 { selector65 } ; 33. if = "IF" expr43 "THEN" { instruction26 } { "ELSIF" expr43 "THEN" { instruction26 } } [ "ELSE" { instruction26 } ] "END" ;34. switch = "SWITCH" expr43 "DO" { "CASE" const_integer17 { "," const_integer17 } ":" { instruction26 } } [ "ELSE" { instruction26 } ] "END" ;35. while = "WHILE" expr43 "DO" { instruction26 } "END" ;36. repeat = "REPEAT" { instruction26 } "UNTIL" expr43 ;37. for = "FOR" qualified_name70 "=" expr43 "TO" expr43 [ "BY" const_integer17 ] "DO" { instruction26 } "END" ;38. loop = "LOOP" { instruction26 } "END" ;39. exit = "EXIT" ;40. try = "TRY" ( assignment27 | function_call28 ) { "CATCH" const_integer17 { "," const_integer17 } ":" { instruction26 } } [ "ELSE" { instruction26 } ] "END" ;41. raise = "RAISE" "ERROR" expr43 expr43 ;42. return = "RETURN" [ expr43 ] ;43. expr = simple_expr44 [ relation47 simple_expr44 ] ; 44. simple_expr = [ "+" | "-" ] term45 { add_operator50 term45 } ;45. term = factor46 { mult_operator51 factor46 } ; 46. factor = "NIL" | boolean48 | number49 | string60 | const_name9 | var_name64 { selector65 } [ substr_selector63 ] | function_call28 | "(" expr43 ")" | "NOT" factor46 | "~" factor46 ;47. relation = "<" | "<=" | "=" | ">=" | ">" | "<>" ;48. boolean = "FALSE" | "TRUE" ;49. number = int_number52 | real_number57 ; 50. add_operator = "+" | "-" | "OR" | "^" | "|" ;51. mult_operator = "*" | "/" | "DIV" | "MOD" | "AND" | "&" ;52. int_number = integer53 | hex_integer55 ; 53. integer = digit54 { digit54 } ; 54. digit = "0".."9" ;55. hex_integer = "0x" hex_digit56 { hex_digit56 } ;56. hex_digit = digit54 | "a".."f" | "A".."F" ;57. real_number = integer53 ( decimals58 [ exponent59 ] | [ decimals58 ] exponent59 ) ; 58. decimals = "." integer53 ;59. exponent = ( "e" | "E" ) [ "+" | "-" ] integer53 ;60. string = "\"" { str_char61 | str_escape62 } "\"" ;61. str_char = " " | "!" | "#".."[" | "]".."~" ;62. str_escape = "\\" ( "\\" | "\"" | "a" | "b" | "n" | "r" | "t" | "x" hex_digit56 hex_digit56 ) ;63. substr_selector = "[" expr43 [ "," expr43 ] "]" ;64. var_name = qualified_name70 ; 65. selector = "[" ( index67 | field_name21 ) "]" | next_elem_in_array66 ;66. next_elem_in_array = "[" "]" ;67. index = expr43 ; 68. name = ( letter69 | "_" ) { letter69 | digit54 | "_" } ;69. letter = "a".."z" | "A".."Z" ;70. qualified_name = [ module_name5 "." ] name68 ; | ||
| |||
| Umberto Salsi | Contatto | Mappa | Home / Indice sezione |