프로그램 사용/antlr2011. 10. 24. 22:35
우분투에서 antlr 실행 파일은 pccts 패키지에 존재한다.

$ antlr
'antlr' 프로그램은 현재 설치되어 있지 않습니다.  다음을 입력하여 이를 설치할 수 있습니다:
sudo apt-get install pccts 

$ antlr
Antlr parser generator   Version 1.33MR33   1989-2001
antlr [options] f1 f2 ... fn
    -CC            Generate C++ output (default=FALSE)
    -cr            Generate cross reference (default=FALSE)
    -ck      ___   Set compressed lookahead depth; fast approximate lookahead
    -e1            Ambiguities/errors shown in low detail (default)
    -e2            Ambiguities/errors shown in more detail
    -e3            Ambiguities for k>1 grammars shown with exact tuples (not lookahead sets)
    -f       ___   Read names of grammar files from specified file
    -fe      ___   Rename err.c
    -fh      ___   Rename stdpccts.h header (turns on -gh)
    -fl      ___   Rename lexical output--parser.dlg
    -fm      ___   Rename mode.h
    -fr      ___   Rename remap.h
    -ft      ___   Rename tokens.h
    -ga            Generate ANSI-compatible code (default=FALSE)
    -gc            Do not generate output parser code (default=FALSE)
    -gd            Generate code to trace rule invocation (default=FALSE)
    -ge            Generate an error class for each non-terminal (default=FALSE)
    -gh            Generate stdpccts.h for non-ANTLR-generated-files to include
    -gk            Generate parsers that delay lookahead fetches until needed
    -gl            Generate line info about grammar actions in C parser
    -glms          Like -gl but replace '\' with '/' in #line filenames for MS C/C++ systems
    -gp      ___   Prefix all generated rule functions with a string
    -gs            Do not generate sets for token expression lists (default=FALSE)
    -gt            Generate code for Abstract-Syntax-Trees (default=FALSE)
    -gx            Do not generate lexical (dlg-related) files (default=FALSE)
    -gxt           Do not generate tokens.h (default=FALSE)
    -k       ___   Set full LL(k) lookahead depth (default==1)
    -o       ___   Directory where all output files should go (default=".")
    -p             Print out the grammar w/o actions (default=no)
    -pa            Print out the grammar w/o actions & w/FIRST sets (default=no)
    -pr            no longer used; predicates employed if present
    -prc     ___   Turn on/off computation of context for hoisted predicates
    -rl      ___   Limit max # of tree nodes used by grammar analysis
    -stdout        Send grammar.c/grammar.cpp to stdout
    -tab     ___   Width of tabs (1 to 8) for grammar.c/grammar.cpp files
    -w1            Set the warning level to 1 (default)
    -w2            Ambiguities yield warnings even if predicates or (...)? block
    -mrhoist ___   Turn on/off k=1 Maintenance Release style hoisting
    -mrhoistk___   Turn on/off k>1 EXPERIMENTAL Maintenance Release style hoisting
    -aa      ___   Ambiguity aid for a rule (rule name or line number)
    -aam           Lookahead token may appear multiple times in -aa listing
    -aad     ___   Limits exp growth of -aa listing - default=1 (max=ck value)
    -info    ___   Extra info: p=pred t=tnodes f=first/follow m=monitor o=orphans 0=noop
    -treport ___   Report when tnode usage exceeds value during ambiguity resolution
    -newAST        In C++ mode use "newAST(...)" rather than "new AST(...)"
    -tmake         In C++ mode use parser's tmake method rather than "ASTBase::tmake(...)"
    -alpha         Provide additional information for "(alpha)? beta" error messages
    -mrblkerr      EXPERIMENTAL change to (...)* and (...)+ syntax error sets
    -nopurify      Don't use the notorious PURIFY macro (replaced by MR23 initial value syntax)
                   to zero return arguments of rules
    -              Read grammar from stdin 

[링크 : http://packages.ubuntu.com/oneiric/pccts]
[링크 : http://packages.ubuntu.com/hardy/antlr]

2011/04/23 - [Programming/언어론] - antlr - ANother Tool for Language Recognition

---
2011.11.06 추가
[링크 : http://www.antlr2.org/pccts133.html]
[링크 : http://www.antlr2.org/1.33/pccts_faq.html]
    [링크 : http://www.polhode.com/pccts.html]
    [링크 : http://www.antlr2.org/book/pcctsbk.pdf]
    [링크 : http://www.antlr2.org/1.33/tutorial.zip]

ubuntu에서 설치가능한 antlr은 PCCTS  버전으로
antlr 1.33MR33 이라고 표기되었듯이 아마도 antlr v1 으로 추정된다.
그런 이유로 이녀석으로 돌려봤자 요즘 문법은(antlr v2 / v3) 인식되지 않으며
기본적으로 생성될 경우 c언어로 출력이 나오게 된다.
$ cat poly.g
poly > [float r]
    :   <<float f;>>
        term>[$r] ( "\+" term>[f] <<$r += f;>> )*
    ;

$ antlr poly.g
Antlr parser generator   Version 1.33MR33   1989-2001
poly.g, line 3: warning: rule term not defined
poly.g, line 3: warning: rule term not defined
poly.g, line 3: warning: rule term not defined

$ cat poly.c 
/*
 * A n t l r  T r a n s l a t i o n  H e a d e r
 *
 * Terence Parr, Will Cohen, and Hank Dietz: 1989-2001
 * Purdue University Electrical Engineering
 * With AHPCRC, University of Minnesota
 * ANTLR Version 1.33MR33
 *
 *   antlr poly.g
 *
 */

#define ANTLR_VERSION	13333
#include "pcctscfg.h"
#include "pccts_stdio.h"
#define zzSET_SIZE 4
#include "antlr.h"
#include "tokens.h"
#include "dlgdef.h"
#include "mode.h"

/* MR23 In order to remove calls to PURIFY use the antlr -nopurify option */

#ifndef PCCTS_PURIFY
#define PCCTS_PURIFY(r,s) memset((char *) &(r),'\0',(s));
#endif

ANTLR_INFO

float
#ifdef __USE_PROTOS
poly(void)
#else
poly()
#endif
{
  float   _retv;
  zzRULE;
  zzBLOCK(zztasp1);
  PCCTS_PURIFY(_retv,sizeof(float  ))
  zzMake0;
  {
  float f;
  zzEXIT(zztasp1);
  return _retv;
fail:
  zzEXIT(zztasp1);
  zzsyn(zzMissText, zzBadTok, (ANTLRChar *)"", zzMissSet, zzMissTok, zzErrk, zzBadText);
  zzresynch(setwd1, 0x1);
  return _retv;
  }
}

$  cat tokens.h
#ifndef tokens_h
#define tokens_h
/* tokens.h -- List of labelled tokens and stuff
 *
 * Generated from: poly.g
 *
 * Terence Parr, Will Cohen, and Hank Dietz: 1989-2001
 * Purdue University Electrical Engineering
 * ANTLR Version 1.33MR33
 */
#define zzEOF_TOKEN 1

#ifdef __USE_PROTOS
extern float poly(void);
#else
extern float poly();
#endif

#endif
extern SetWordType setwd1[];


$  cat err.c
/*
 * A n t l r  S e t s / E r r o r  F i l e  H e a d e r
 *
 * Generated from: poly.g
 *
 * Terence Parr, Russell Quong, Will Cohen, and Hank Dietz: 1989-2001
 * Parr Research Corporation
 * with Purdue University Electrical Engineering
 * With AHPCRC, University of Minnesota
 * ANTLR Version 1.33MR33
 */

#define ANTLR_VERSION	13333
#include "pcctscfg.h"
#include "pccts_stdio.h"
#define zzSET_SIZE 4
#include "antlr.h"
#include "tokens.h"
#include "dlgdef.h"
#include "err.h"

ANTLRChar *zztokens[3]={
	/* 00 */	"Invalid",
	/* 01 */	"@",
	/* 02 */	"\\+"
};
SetWordType setwd1[3] = {0x0,0x1,0x0};

$  cat parser.dlg
<<
/* parser.dlg -- DLG Description of scanner
 *
 * Generated from: poly.g
 *
 * Terence Parr, Will Cohen, and Hank Dietz: 1989-2001
 * Purdue University Electrical Engineering
 * With AHPCRC, University of Minnesota
 * ANTLR Version 1.33MR33
 */

#define ANTLR_VERSION	13333
#include "pcctscfg.h"
#include "pccts_stdio.h"
#include "antlr.h"
#include "tokens.h"
#include "dlgdef.h"
LOOKAHEAD

void
#ifdef __USE_PROTOS
zzerraction(void)
#else
zzerraction()
#endif
{
	(*zzerr)("invalid token");
	zzadvance();
	zzskip();
}
>>


%%START

@
	<<
		NLA = 1;
	>>

\+
	<<
		NLA = 2;
	>>

%%
[링크 : http://www.rubyholic.com/Languages/PCCTS/Grammars.html

'프로그램 사용 > antlr' 카테고리의 다른 글

java를 이용한 antlr AST 탐색하기(AST navigation)  (0) 2011.11.02
ANTLR IDE 2.1.2 for ANTLR 3.0 above  (0) 2011.11.02
antlrworks  (0) 2011.11.02
antlr + eclipse & java  (0) 2011.10.30
antlr - ANother Tool for Language Recognition  (0) 2011.04.23
Posted by 구차니