|
Re: [cedet-semantic] using semantic/wisent as a parser
From: Joe Corneli <jcorneli@ma...> - 2004-05-07 15:22
|
semantic-lex uses the current Emacs syntax table to recognize
language comments.
I recommend you to read the chapter on syntax tables on the "Emacs
Lisp Reference Manual", to understand the meaning of the `1', `2',
`3', `4' syntax flags.
Ok, thanks - I had been looking at the documentation string for
modify-syntax-entry and for some reason thought that the different
properties were to be added sequentially. However, while that
didn't work, neither did the revised syntax table that you
suggested, which follows the pattern set forth in the ELISP manual.
Parenthesis-like tokens are better handled by an auto-generated
<block> analyzer. To declare it you should add:
%type <block>
%token <block> PAREN_BLOCK "(LPAREN RPAREN)"
%token <block> BRACE_BLOCK "(LBRACE RBRACE)"
%token <block> BRACK_BLOCK "(LBRACK RBRACK)"
to indicate the possible blocks made of these parenthesis
delimiters.
OK, this (and grammer elements in the style of wisent-python) appear
to be working for me. Thanks.
Anyone willing to take a look and try to understand why comments
aren't being ignored properly? Here are my new files.
;;; simple-mode.el -- major mode for a simple language
;;; Semantic parsing support
(require 'semantic-wisent)
(require 'simple-wy)
;;;###autoload
(defun semantic-default-simple-setup ()
"Set up a buffer for semantic parsing of a SIMPLE language."
;; Do some useful things.
(setq
semantic-ignore-comments t
document-comment-start "/\\*"
document-comment-end "\\*/")
;; Install the parser
(simple-wy--install-parser)
;; Setup the lexer
(setq semantic-lex-analyzer 'simple-lexer
;; Do a full depth lexical analysis.
semantic-lex-depth nil))
;;;###autoload
(add-hook 'simple-mode-hook 'semantic-default-simple-setup)
;;; post parser processing
;; I'm not sure why Bovine is not getting rid of comments -- I suppose
;; I haven't dealt with them properly. I was sort of under the
;; impression that this would be handled automatically after setting
;; the document comment start and end -- but that doesn't seem to be
;; the case. Maybe I just need to remove comments as part of my own
;; preprocessing phase. Wouldn't be hard to do.
(defun clean-up-parser-output ()
(interactive)
(set-buffer (get-buffer "*Parser Output*"))
(flush-lines "overlay")
(flush-lines "\"expr\" expr")
(goto-char (point-min))
(replace-regexp " (:value \"\\|\")$" ""))
(defun novinate ()
(interactive)
(bovinate)
(clean-up-parser-output))
;;; simple major mode
(defvar simple-mode-syntax-table
(let ((table (make-syntax-table (standard-syntax-table))))
(modify-syntax-entry ?\+ "." table) ;; Operator PLUS
(modify-syntax-entry ?\- "." table) ;; Operator MINUS
;; also deals with comments
(modify-syntax-entry ?\* ". 23" table) ;; Operator MULT
(modify-syntax-entry ?\/ ". 14" table) ;; Operator DIV
;; deal with brackets
(modify-syntax-entry ?\( "()" table)
(modify-syntax-entry ?\{ "(}" table)
(modify-syntax-entry ?\[ "(]" table)
(modify-syntax-entry ?\) ")(" table)
(modify-syntax-entry ?\} "){" table)
(modify-syntax-entry ?\] ")[" table)
table)
"Syntax table used in simple mode buffers.
Define operators as punctuations.")
;;;###autoload
(define-derived-mode simple-mode fundamental-mode "simple")
;;;###autoload
(add-to-list 'auto-mode-alist '("\\.simple\\'" . simple-mode))
;(defvar simple-mode-hook nil)
(provide 'simple-mode)
;;; simple-mode.el ends here
;;; simple.wy -- LALR grammar for (simplified) Tiger
%package simple-wy
;; Not really necessary, as it is the default start symbol
%start expr
%start function_parameters
;; %type <symbol>
;; %token <symbol> symbol "[A-Za-z][_A-Za-z0-9]*"
%type <keyword>
%keyword WHILE "while"
%keyword FOR "for"
%type <open-paren>
%token <open-paren> LPAREN "("
%token <open-paren> LBRACE "{"
%token <open-paren> LBRACK "["
%type <close-paren>
%token <close-paren> RPAREN ")"
%token <close-paren> RBRACE "}"
%token <close-paren> RBRACK "]"
%type <block>
%token <block> PAREN_BLOCK "(LPAREN RPAREN)"
%token <block> BRACE_BLOCK "(LBRACE RBRACE)"
%token <block> BRACK_BLOCK "(LBRACK RBRACK)"
%%
;; For use with Semantic, must return valid semantic tags!
expr
: ;; Empty
| PAREN_BLOCK
;;(TAG "expr" 'expr :value $2)
(EXPANDFULL $1 function_parameters)
| FOR
(TAG "expr" 'expr :value $1)
;
;; parameters: '(' [varargslist] ')'
function_parameters
: LPAREN
()
| RPAREN
()
;; | function_parameter COMMA
| function_parameter RPAREN
;
function_parameter
: WHILE
(TAG "function_parameter" 'function_parameter :value $1)
;
%%
(define-lex simple-lexer
"Simple lexical analyzer."
semantic-lex-ignore-whitespace
semantic-lex-ignore-newline
semantic-lex-ignore-comments
;;;; Auto-generated analyzers.
simple-wy--<block>-block-analyzer
simple-wy--<keyword>-keyword-analyzer
;;;;
semantic-lex-default-action)
;;; simple.wy ends here
|
| Thread | Author | Date | |
|---|---|---|---|
| Re: [cedet-semantic] using semantic/wisent as a parser | David PONCE <david.ponce@wa...> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|