Sie sind auf Seite 1von 6

Syntax Diagrams

2004/03/11 13:54:42

Introduction

A convenient way to visualise a grammar is to imagine that it is a railway system, where any valid path taken by a train creates a sentence belonging to the language of the grammar. These pictorial representations of grammars are called railroad diagrams or more commonly syntax diagrams. This note explores syntax diagrams for a subset of the Java language.

Types

To start with something simple, consider the syntax for specifying types in Java. In the following we can see two examples: int and String[]. int iOffset; static public void main(String[] args) We can generalize what we see and and present it using the syntax diagrams in Figure 1.

type_specifier boolean char int float

type ] type_specifier [

double class_name interface_name

Figure 1: Two syntax diagrams: the left is the type syntax diagram and the right is the type specifier syntax diagram. 1

School of Informatics, University of Edinburgh

Computer Science 1

[0304]

A terminal is something which is written literally, such as int, while a non-terminal is expanded into other terminals and non-terminals. In this notation, non-terminals appear in boxes and terminals are circled.

In this gure we see that types are specied with a type specifier followed by a number of optional []. type specifier can be any native Java type or the name of a class or interface. [] species that the type is an array. For the purposes of this note class name and identifier name mean the same as identifier. String[] is also a valid type since it is generated by choosing the path through class name and by taking the optional type path that adds []. The path through class name is chosen because String is a class and the path through the [ and then ] is taken because in our example we have declared an array of String.

Identiers

Java identiers are the names given for classes, interfaces, packages, methods, and variables. Properly formed identiers begin with a letter, underscore, or dollar sign, are case sensitive and have no maximum length. Figure 2 shows the syntax diagram corresponding to a Java identier.
identifier a..z,$,_,0..9,unicode character over 00C0 a..z,$,_

Figure 2: Identier syntax diagram.

Modiers

Now let us look at another important part of Java: modiers. Figure 3 shows the syntax diagram corresponding to modiers.
modifier public private protected static final

Figure 3: Modier syntax diagram. 2

School of Informatics, University of Edinburgh

Computer Science 1

[0304]

Variable Declaration

Now we can combine what we have learned so far (types, identiers and modiers) into something more interesting: variable declarations. The following example shows two variable declarations. int i, j, k; private static HashMap data[]; Figure 4 6 show the syntax diagram for variable declaration in Java. Try following the syntax diagrams using the example.
variable_declaration modifier type variable_declarator variable_declarator , ;

Figure 4: Syntax diagram for variable declaration in Java. The variable declarator non-terminal contains an identier and an optional initialiser. Additionally, the variable declarator can specify that the variable is an array. Note that this is different than a type specied array as shown in Section 2. Figure 5 gives a closer look at this.
variable_declarator ] identifier [ = variable_initializer

Figure 5: Variable declarator syntax. Finally, the variable initializer (Figure 6) is responsible for giving the variable its initial values.
variable_initializer expression variable_initializer variable_initializer { } , ,

Figure 6: Syntax diagram for variable initializer.

School of Informatics, University of Edinburgh

Computer Science 1

[0304]

Reference

Figure 7 9 show syntax diagrams for commonly used components of Java. Note that many of these examples are simplied for brevity.
statement statement_block variable_declaration expression if_statement while_statement for_statement switch_statement expression return ;
statement_block statement { }

expression numeric_expression testing_expression logical_expression string_expression literal_expression identifier

Figure 7: Statements and expressions.

School of Informatics, University of Edinburgh


testing_expression > < expression >= <= == != expression

Computer Science 1

[0304]

numeric_expression ++ expression ++ + += = expression * *= / /= % %=


import_statement package_name import class_name interface_name . * ; ;

expression

expression

if_statement else if ( expression ) statement statement

5 Figure 8: More statements and expressions.

School of Informatics, University of Edinburgh


for_statement variable_declaration for ( expression ;
logical_expression ! expression & &= | |= expression ^ ^= && ||= % %= expression
true false ?

Computer Science 1

[0304]

expression ;

expression ; ) statement

expression

expression

expression

class_declaration interface_name modifier class identifier extends class_name implements interface_name { , field_declaration }

method_declaration modifier type identifier ( parameter_list ) ] [ statement_block ;

parameter ] type identifier [

parameter_list parameter parameter ,

Figure 9: A couple more statements and expressions. Class declarations, and methods. 6

Das könnte Ihnen auch gefallen