Docsity

Autohighlighter: Syntax Highlighting Tool with BNF Grammars and Lexical Specs, Papers of Electrical and Electronics Engineering

Toggle sidebar
bg1
Automatic syntax highlighter generation
Allen, S. T.; Williams, S. R.
December 9, 2005
Abstract
Autohighlight is a tool that produces syntax highlighting modules for user-defined BNF grammars
and lexical specifications for Emacs and VIM. Autohighlight can color all literals in a sample document,
plus a strictly-defined subset of the nonterminal symbols defined in the BNF grammar.
1 Definitions
Autohighlight is a meta-level tool, so the terms used to talk about it are sometimes confusing. Autohighlight
produces code that helps the editor color the words in a file, called the sample, which is a conforming example
of a given language. The language spec tells you the syntax and lexical properties of the language.
Autohighlight is interested in two parts of the language spec, the BNF grammar of the language (the
grammar), and the lexical specification of the basic symbols of the language (the lexical spec). The lexical
specification specifies the regular syntax of lexical symbols.
The BNF grammar of a language is made of a list of productions, which name a non-terminal symbol on
the left-hand side of a rule and tell on the right-hand side of the rule what symbols (both terminals (literals
or lexical symbols) and non-terminals) the non-terminal may expand to in the sample.
Expanding a symbol sto terminals on a non-terminal symbol sis accomplished by expanding all symbols
in all the productions of suntil only terminals remain. Note that not all non-terminal symbols can be
expanded to terminals—expanding the root of a useful language should produce all the possible samples of
that language, which is infinite in many cases.
Within the grammar, Autohighlight distinguishes among different classes of non-terminal symbols. A
non-terminal would be terminal-equivalent if it produces a single terminal when expanded to terminals. The
terminal-equivalent regex of a terminal-equivalent symbol sis a regular expression matching the terminals
that the terminal-equivalent symbol smay expand to. A closely related action is to find the left-most or
right-most expansion regex of a given non-terminal s. This regex matches the end of all productions of s
when sis expanded to terminals.
Lastly, Autohighlight employs a few more terms related to the BNF grammar. The total context of a
terminal-equivalent symbol sis a set of pairs of expansion regexes that match the symbol’s context when it
appears in the sample.
2 Motivation
When we were first introduced to the Eli compiler generation system, Scott immediately hacked out an Emacs
mode for highlighting Eli’s FunnelWeb files. Writing syntax highlighting code is painful, because a lot of
effort is required to keep the highlighter in sync with the language spec. Additionally, lots of iterations are
required to produce a highlighter that deals with coloring a symbol which has the same lexical specification
as a symbol which should be colored differently, as the context of the symbol must be taken into account.
Since the syntax highlighting specification is written by intuition and not by a particular algorithm, there’s
always the danger that a context will be missed simply because the author of the syntax highlighting spec
has never generated a sample containing that particular context.
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Autohighlighter: Syntax Highlighting Tool with BNF Grammars and Lexical Specs and more Papers Electrical and Electronics Engineering in PDF only on Docsity!

Automatic syntax highlighter generation

Allen, S. T.; Williams, S. R.

December 9, 2005

Abstract Autohighlight is a tool that produces syntax highlighting modules for user-defined BNF grammars and lexical specifications for Emacs and VIM. Autohighlight can color all literals in a sample document, plus a strictly-defined subset of the nonterminal symbols defined in the BNF grammar.

1 Definitions

Autohighlight is a meta-level tool, so the terms used to talk about it are sometimes confusing. Autohighlight produces code that helps the editor color the words in a file, called the sample, which is a conforming example of a given language. The language spec tells you the syntax and lexical properties of the language. Autohighlight is interested in two parts of the language spec, the BNF grammar of the language (the grammar), and the lexical specification of the basic symbols of the language (the lexical spec). The lexical specification specifies the regular syntax of lexical symbols. The BNF grammar of a language is made of a list of productions, which name a non-terminal symbol on the left-hand side of a rule and tell on the right-hand side of the rule what symbols (both terminals (literals or lexical symbols) and non-terminals) the non-terminal may expand to in the sample. Expanding a symbol s to terminals on a non-terminal symbol s is accomplished by expanding all symbols in all the productions of s until only terminals remain. Note that not all non-terminal symbols can be expanded to terminals—expanding the root of a useful language should produce all the possible samples of that language, which is infinite in many cases. Within the grammar, Autohighlight distinguishes among different classes of non-terminal symbols. A non-terminal would be terminal-equivalent if it produces a single terminal when expanded to terminals. The terminal-equivalent regex of a terminal-equivalent symbol s is a regular expression matching the terminals that the terminal-equivalent symbol s may expand to. A closely related action is to find the left-most or right-most expansion regex of a given non-terminal s. This regex matches the end of all productions of s when s is expanded to terminals. Lastly, Autohighlight employs a few more terms related to the BNF grammar. The total context of a terminal-equivalent symbol s is a set of pairs of expansion regexes that match the symbol’s context when it appears in the sample.

2 Motivation

When we were first introduced to the Eli compiler generation system, Scott immediately hacked out an Emacs mode for highlighting Eli’s FunnelWeb files. Writing syntax highlighting code is painful, because a lot of effort is required to keep the highlighter in sync with the language spec. Additionally, lots of iterations are required to produce a highlighter that deals with coloring a symbol which has the same lexical specification as a symbol which should be colored differently, as the context of the symbol must be taken into account. Since the syntax highlighting specification is written by intuition and not by a particular algorithm, there’s always the danger that a context will be missed simply because the author of the syntax highlighting spec has never generated a sample containing that particular context.

3 Analysis flow

Autohighlight follows the general analysis flow of any compiler:

  1. Tokenization
  2. Parsing
  3. Context-checking

(a) Name analysis (b) Type analysis (c) Colorability analysis

  1. Output

We’ll delve further into the details of all the items than would be necessary in an Eli-generated compiler because we implemented our parser using Python. Despite using a different technology, many of the same underlying concepts from Eli apply to our Python implementation. We began our implementation using Eli, abandoning the method when we reached item 3, but the grammar of the Autohighlight specifications didn’t change. In order to provide a basis for understanding the Autohighlight tokenizer and parser, figure 3 gives the concrete grammar and lexical specification we wrote for Eli.

3.1 Tokenization

The tokenizer is a finite state machine encapsulated in a Python generator. Because it is a generator, to use it is a matter of initializing it (using a stream or a filename), and then simply calling next(). Each call to next will return the next token. It is also possible to use it as a source list in a for-in loop. A generator must have a method iter which produces an object that has a next method. The next method is called repeatedly to get the next element in the sequence the generator represents until the next method raises a StopIteration exception.

c l a s s T o k e n i z e r :

... def next ( s e l f ) : s e l f. token , s e l f. s l i n e , s e l f. s c o l , s e l f. c ha r = ”” , s e l f. l i n e , s e l f. c o l , ’ ’

while True : s e l f. char = s e l f. stream. re a d ( 1 ) r e t v a l = s e l f. t r a n s i t i o n ( ) i f s e l f. char == ’ \n ’ : s e l f. s e t C u r s o r ( s e l f. l i n e + 1 , 0 ) e l s e : s e l f. s e t C u r s o r ( s e l f. l i n e , s e l f. c o l + 1 ) i f r e t v a l : return r e t v a l i f s e l f. char == ’ ’ : r a i s e S t o p I t e r a t i o n ( ) The state machine has several transition actions when switching between states. By looking at the transition actions listed below, you can tell the state machine is not “clean” and some of the state is kept in the tokenizer object as member data instead of encapsulated in the state number. This is for practical reasons.

c l a s s T o k e n i z e r :

... def add ( s e l f ) :

s e l f. token += s e l f. char return None def tok ( s e l f ) : return Token ( s e l f. s l i n e , s e l f. s c o l , s e l f. token ) def push ( s e l f ) : i f s e l f. char == ’ ’ : return s e l f. s e t C u r s o r ( s e l f. l i n e , s e l f. c o l − 1) i f s e l f. c o l < 0 or s e l f. c ha r == ’ \n ’ : s e l f. s e t C u r s o r ( s e l f. l i n e − 1 , 0 ) s e l f. stream. s e e k ( − 1 , 1 ) def s t o p ( s e l f ) : r a i s e S t o p I t e r a t i o n ( ) def noop ( s e l f ) : return None def r e s e t ( s e l f ) : s e l f. s l i n e , s e l f. s c o l = s e l f. l i n e , s e l f. c o l return None def s t r a n g e c h a r ( s e l f ) : r a i s e UnexpectedCharacter ( s e l f. l i n e , s e l f. c o l , s e l f. c har ) def e n d i n s t r ( s e l f ) : r a i s e E o f I n S t r i n g ( s e l f. s l i n e , s e l f. s c o l )

The state transition table is an array, where each element corresponds to a state. Each element is a list of transitions out of the state. Each transition is a tuple whose head is either a string to match exactly or a regex to match that indicates whether this transition should be used. The first transition whose head matches is used. The second element in the transition tuple indicates the destination state number, and the remainder of the tuple is a list of actions to take on leaving. The c function is a helper function that compiles its argument into a regular expression object.

c l a s s T o k e n i z e r :

... t r a n s i t i o n s = [ \

s t a t e 0 : i n i t i a l s t a t e \

[ ( c ( ” [ A−Za−z ] ” ) , 1 , r e s e t , add ) , ( ’ ’ , 0 , s t o p ) , ( c ( ’ [0 −9] ’ ) , 5 , r e s e t , add ) , ( ’ $ ’ , 2 , r e s e t , add ) , ( ” ’ ” , 3 , r e s e t , add ) , ( c ( ’ [ ] [ { } : ;. ] ’ ) , 0 , r e s e t , add , tok ) , ( c ( ’ \ s ’ ) , 0 , noop ) , ( c ( ’. ’ ) , 0 , s t r a n g e c h a r ) ] , \

s t a t e 1 : a c c u m u l a t i n g i d e n t i f i e r s \

[ ( c ( ”[−A−Za−z0 − 9 ] ” ) , 1 , add ) , ( ’ ’ , 0 , tok ) , ( c ( ’. ’ ) , 0 , push , tok ) ] , \

s t a t e 2 : a c c u m u l a t i n g r e g e x e s \

[ ( ’ ’ , 0 , push , tok ) , ( c ( ’ \ s ’ ) , 0 , tok ) , ( c ( ’. ’ ) , 2 , add ) ] , \

s t a t e 3 : a c c u m u l a t i n g s t r i n g s \

[ ( ’ \ \ ’ , 4 , add ) , ( ” ’ ” , 0 , add , tok ) , ( ’ ’ , 0 , e n d i n s t r ) , ( c ( ’. ’ ) , 3 , add ) ] , \

s t a t e 4 : e s c a p i n g s t r i n g s \

[ ( ’ ’ , 0 , e n d i n s t r ) , ( c ( ’. ’ ) , 3 , add ) ] , \

s t a t e 5 : i n t e g e r s \

[ ( c ( ” [0 −9] ” ) , 5 , add ) , ( ’ ’ , 0 , tok ) , ( c ( ’. ’ ) , 0 , push , tok ) ] ]

The transition notation is complex, so a member function transition exists that takes the input character and the current state, determines the transition, takes the transition actions (accumulating their output).

c l a s s T o k e n i z e r :

... def t r a n s i t i o n ( s e l f ) : s t a t e d e f = s e l f. t r a n s i t i o n s [ s e l f. s t a t e ] for path in s t a t e d e f : pat , d e s t = path [ : 2 ] i f type ( pat ). n a m e == ” s t r ” and pat == s e l f. c ha r or type ( pat ). n a m e == ” SRE Pattern ” and pat. match ( s e l f. c ha r ) :

for a c t i o n in path [ 2 : ] : r e t v a l = a c t i o n ( s e l f ) s e l f. s t a t e = d e s t return r e t v a l r a i s e Exc e pt ion ( ”No matching path f o r c ha r %s from s t a t e %d. ” % ( s e l f. char , s e l f. s t a t e ) )

3.2 Parsing and basic analysis

Our parsing routine can perform basic name and type analysis for simple cases, so the two are integrated. See Autohighlight::parse for details on the highest level of parsing and simple analysis. Once the parse method has finished running, the Autohighlight object has a ColorDefinitions hash containing entities representing colors defined in the input file. It also contains an OrderedColorMappings list representing the color requests the user made. This is an ordered list, so that color mappings will be output in the order specified by the user, to allow the user to specify more specific color mappings first, and more general color mappings last, to help the editors properly cope with ambiguous colorings. Additionally, the object also contains a GlobalSymbolDict hash containing all the grammar and lexical symbols defined. After parse is run, the only remaining step in the analysis phase is to determine colorability, that is, to determine whether any coloring requests the user has given meet the criteria for colorable symbols.

3.2.1 Parsing the lexical section

The method parse_lexical_symbols illustrated below works by building up a stack until a complete lexical specification is built up. When a period is found, it checks that the symbol isn’t being redefined, and then inserts it into the GlobalSymbolDict hash.

c l a s s A u t o h i g h l i g h t :

... def p a r s e l e x i c a l s y m b o l s ( s e l f ) : s t a c k = [ ] s e l f. t o k e n i z e r. next ( ). must be ( ’ { ’ ) for token in s e l f. t o k e n i z e r : s t a c k + = [ token ] i f token. t e x t == ”. ” : s t a c k [ 0 ]. a s s e r t s y m b o l n a m e ( ) s t a c k [ 1 ]. must be ( ’ : ’ ) s t a c k [ 2 ]. must match ( ’ ˆ\ $ ’ , ” r e g u l a r e x p r e s s i o n ” )

Name a n a l y s i s

i f s t a c k [ 0 ]. t e x t in s e l f. GlobalSymbolDict : o r i g i n a l D e f = s e l f. GlobalSymbolDict [ s t a c k [ 0 ]. t e x t ]. d e f i n i n g t o k e n r a i s e Exc e p t i o n ( ”Symbol %s r e d e f i n e d a t %d,%d. O r i g i n a l l y a t %d,%d”

s = Symbol ( s t a c k [ 0 ] ) s. i s g l a = True s. r e g e x = s t a c k [ 2 ]. t e x t [ 1 : ] s e l f. GlobalSymbolDict [ s t a c k [ 0 ]. t e x t ] = s s t a c k = [ ] e l i f token. t e x t == ”{” : r a i s e Exc e pt ion ( ” Unexpected %s ” % token ) e l i f token. t e x t == ”}” : i f l e n ( s t a c k ) > 1 : r a i s e E x c e p t i o n ( ” U n f i n i s h e d l e x i c a l s p e c i f i c a t i o n b e g #pp = p p r i n t. P r e t t y P r i n t e r ( )

3.5 User Manual

To use autohighlighter, first you must generate an autohighlighter file, with extension .ah. This file specifies not only the specifics of the language you wish to color, but how you would like that language to be colored. There are three main sections to the autohighlighter file: the gla section, which contains regular expressions for terminal symbols, the concrete syntax tree section, which contains the BNF grammar for the language, and the coloring section, which provides definitions of custom colors, and dictates which portions of the language will be colored which color. The gla section should consist of lines of the following format: i d e n t i f i e r : $ r e g u l a r e x p r e s s i o n. The BNF grammar section should consist of lines similar to the following format: symbol : symbol ’ l i t e r a l ’ symbol. where the right hand side may be any combination of symbols and literals, with literals enclosed in single quotes. The coloring section should consist of color definitions, and color mappings. Color definitions are of the form:

colorname { c o l o r : re d ; background : b l u e ; t e x t −d e c o r a t i o n : u n d e r l i n e ; }

The complete list of attributes, with possible values are: f o n t −f a m i l y : < fontname> f o n t −s t y l e : normal , i t a l i c f o n t −w e ig ht : bold , normal f o n t −s i z e : < p o i n t s > t e x t −d e c o r a t i o n : u n d e r l i n e , o v e r l i n e , l i n e −through , i n v e r s e

c o l o r : Black , DarkBlue , DarkGreen , DarkCyan , DarkRed , DarkMagenta , Brown , DarkYellow , LightGray , LightGrey , Gray , Grey , DarkGray , DarkGrey , Blue , LightBlue , Green , LightGreen , Cyan , LightCyan , Red , LightRed , Magenta , LightMagenta , Yellow , LightYellow , White

background−c o l o r : Black , DarkBlue , DarkGreen , DarkCyan , DarkRed , DarkMagenta , Brown , DarkYellow , LightGray , LightGrey , Gray , Grey , DarkGray , DarkGrey , Blue , LightBlue , Green , LightGreen , Cyan , LightCyan , Red , LightRed , Magenta , LightMagenta , Yellow , LightYellow , White

Color mappings are of the form:

colorname : symbol ’ l i t e r a l ’ symbol. There are several predefined color names that map appropriately to the built-in recommended highlighting colors of both editors. These are:

Comment Use this to highlight anything

Constant For integers and other constants

String For strings

VariableName For identifiers

FunctionName For function identifiers

Keyword For distinguished and useful literals listed in the grammar.

Type For type identifiers

None Don’t color

Error Should be flagged as an error

Once the autohighlighter file has been generated, it should be given a .ah file suffix. Then, the compiler should be run. The full command line syntax is as follows:

Usage : python ah. py [ OPTION ]... [ FILE ] G e n e r a t e s t he s p e c i f i e d syntax h i g h l i g h t i n g f i l e s from t h e g i v e n i n p u t FILE.

Options : −h, −− h e l p P r i n t s t h i s h e l p −−vim G e n e r a t e s a vim syntax h i g h l i g h t i n g f i l e −−emacs G e n e r a t e s an emacs f o n t l o c k i n g f i l e −−e r r o r −c h e c k i n g H i g h l i g h t a l l symbols not c u r r e n t l y b e i n g c o l o r e d as e r r o r s ( c u r r