Σύνταξη (γλώσσα προγραμματισμού): Διαφορά μεταξύ των αναθεωρήσεων

Περιεχόμενο που διαγράφηκε Περιεχόμενο που προστέθηκε
Χωρίς σύνοψη επεξεργασίας
Γραμμή 10:
Η σύνταξη των γλωσσών προγραμματισμού βασισμένων σε κείμενο συνήθως ορίζεται από ένα συνδυασμό [[κανονική έκφραση|κανονικών εκφράσεων]] (για τη [[λεκτική ανάλυση|λεκτική]] δομή) και μορφής Μπάκους-Ναούρ (για τη [[γραμματικής χωρίς συμφραζόμενα|γραμματική]] δομή) για τον επαγωγικό ορισμό των συντακτικών κατηγοριών (μη-τερματικών) και των ''τερματικών'' συμβόλων. Οι συντακτικές κατηγορίες ορίζονται από κανόνες που ονομάζονται ''παραγωγές'' (''productions''), οι οποίοι καθορίζουν τις τιμές που ανήκουν σε μια συγκεκριμένη συντακτική κατηγορία.<ref name="eopl"/> Τα τερματικά σύμβολα είναι σταθεροί χαρακτήρες ή ακολουθίες χαρακτήρων (για παράδειγμα λέξεις-κλειδιά όπως οι ''define'', ''if'', ''let'', ή ''void'') από τα οποία κατασκευάζονται τα έγκυρα προγράμματα.
 
Ακολουθεί μια απλή γραμματική, βασισμένη στη [[Lisp]], που ορίζει κανόνες παραγωγής για τις συντακτικές κατηγορίες ''expressionέκφραση'', ''atomάτομο'', ''numberαριθμός'', ''symbolσύμβολο'', και ''listλίστα'':
 
:<code>
expressionέκφραση ::= atom άτομο | listλίστα<br/>
atom άτομο ::= numberαριθμός | symbolσύμβολο<br/>
number αριθμός ::= [+-]?['0'-'9']+<br/>
symbol σύμβολο ::= ['A'-'Z'<nowiki>'</nowiki>a'-'z'].*<br/>
list λίστα ::= '(' expressionέκφραση* ')'<br/>
</code>
 
Η γραμματική αυτή ορίζει τα εξής:
<!--
* μια ''έκφραση'' είναι είτε ένα ''άτομο'' είτε μια ''λίστα'',
* ένα ''άτομο'' είναι είτε ένας ''αριθμός'' είτε ένα ''σύμβολο'',
* ένας ''αριθμός'' είναι μια συνεχής ακολουθία από ένα ή περισσότερα δεκαδικά ψηφία, τα οποία προαιρετικά μπορεί να έχουν ένα σημείο συν ή πλην στην αρχή τους,
* ένα ''σύμβολο'' είναι ένα γράμμα που ακολουθείται από μηδέν ή περισσότερους χαρακτήρες (εκτός από κενά), και
* μια ''λίστα'' είναι ένα ταιριασμένο ζευγάρι παρενθέσεις, με μηδέν ή περισσότερες ''εκφράσεις'' μέσα σε αυτές.
Εδώ, τα δεκαδικά ψηφία, οι χαρακτήρες (πεζά και κεφαλαία), και οι παρενθέσεις, είναι τα τερματικά σύμβολα.
 
Τα επόμενα είναι παραδείγματα καλώς σχηματισμένων ακολουθιών από λεκτικές μονάδες για αυτήν τη γραμματική: '<code>12345</code>', '<code>()</code>', '<code>(a b c232 (1))</code>'
This grammar specifies the following:
* an ''expression'' is either an ''atom'' or a ''list'';
* an ''atom'' is either a ''number'' or a ''symbol'';
* a ''number'' is an unbroken sequence of one or more decimal digits, optionally preceded by a plus or minus sign;
* a ''symbol'' is a letter followed by zero or more of any characters (excluding whitespace); and
* a ''list'' is a matched pair of parentheses, with zero or more ''expressions'' inside it.
Here the decimal digits, upper- and lower-case characters, and parentheses are terminal symbols.
 
The following are examples of well-formed token sequences in this grammar: '<code>12345</code>', '<code>()</code>', '<code>(a b c232 (1))</code>'
 
<!--
The grammar needed to specify a programming language can be classified by its position in the [[Chomsky hierarchy]]. The syntax of most programming languages can be specified using a Type-2 grammar, i.e., they are [[context-free grammar]]s.<ref>{{cite book|author = [[Michael Sipser]] | year = 1997 | title = Introduction to the Theory of Computation | publisher = PWS Publishing | isbn = 0-534-94728-X}} Section 2.2: Pushdown Automata, pp.101&ndash;114.</ref> However, there are exceptions. In some languages like Perl and Lisp the specification (or implementation) of the language allows constructs that execute during the parsing phase. Furthermore, these languages have constructs that allow the programmer to alter the behavior of the parser. This combination effectively blurs the distinction between parsing and execution, and makes syntax analysis an [[undecidable problem]] in these languages, meaning that the parsing phase may not finish. For example, in Perl it is possible to execute code during parsing using a <code>BEGIN</code> statement, and Perl function prototypes may alter the syntactic interpretation, and possibly even the syntactic validity of the remaining code.<ref>The following discussions give examples:
* [http://www.jeffreykegler.com/Home/perl-and-undecidability Perl and Undecidability]