8.1 The Role of Regular Expressions

Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  8.   Parsing Regular Expressions


Regular expressions appear frequently in support of user interfaces. For example, a user might want to see all the files that end with ".txt" or all the books whose titles include "Java" and "Parsers" . When you allow a user to specify such a pattern, you are using regular expressions to let the user create a new little language. You might decide, for example, to allow your user to use a tilde to mean zero or more characters . If your user types "~.txt" , he or she is defining the language of all strings that end with ".txt" .

The tools you provide your user for matching are a metalanguage , a language for defining new languages. Typically, developers provide users with symbols such as " ~ " and " " to indicate which patterns of text the user is interested in. These symbols are the terminals in the metalanguage the developer provides. The user specifies a new language (a new set of strings) by typing text that uses the metalanguage symbols.

Because the user who specifies a pattern is specifying a new language, your code must take the user's text and parse it. This is the parser you write in advance of prompting your user. Your parser will take the user's input and create a new parser to recognize the language your user specifies. For example, you might write a parser to recognize patterns of book titles. When your user enters the string "Java Parsers" , your parser will recognize the two words and the vertical bar. Your parser then must build a new parser to recognize the language (or set of strings) that the user is specifying. You take the user's input, build a new parser, and then apply this to the books in your database to see which titles lie within this new, fleeting, little language.

The result of matching a parser against an input string is an object that is based on the parser's recognition of the string. For example, a parser for a coffee markup language will return a new Coffee object that reflects the description the parser recognizes in a markup string. In the case of a metalanguage, the object your parser returns is a new parser.


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net