Outils pour utilisateurs

Outils du site


states

Ceci est une ancienne révision du document !


State Machine

fsm2.in
//////////////////////////////////////////////////////////////////////////////////////////////////////
//Les différents états avec actions sur état
	1 :I,sortie4;
 	2 :I,sortie3:R,sortie4=IN;
	3 :I,sortie3:R,sortie4=IN;
////////////////////////////////////////////////////////////////////////////////////////////////////// 
//Transitions et actions sur transitions
	1 -> 2 ?NOT IN;
	1 -> 3 ?IN; 
	3 -> 2 ?NOT IN;
	2 -> 3 ?IN;
	3 -> 1 ?IN:I,sortie1:S,sortie2;
//////////////////////////////////////////////////////////////////////////////////////////////////////  
//Transitions avec état d'origine vide pour reset synchrone, actions sur reset
	->2 ?SRESET AND IN;
	->3 ?SRESET AND NOT IN: I,sortie2: R,sortie5=entree2;
States.g4
grammar States;
r  : line+ ;          //a state machine is many lines
 
line : state 
     | transition
     | reset_transition
     ;
 
state   :  id (':' action)* ';'   ;         	//state (with  action(s))
 
action :  (action_type ',')? ID ('=' expression)? ;
 
action_type : 'R' | 'S' | 'I' ;
 
transition   	  :  id '->' id ('?' condition)? (':' action)* ';'   ;         	//transition (with  action(s)) 
reset_transition  :   '->' id ('?' condition)? (':' action)* ';'   ;         	//transition (with  action(s)) 
 
condition :  id ( id)*  ;
expression :  id ( id)*  ;
 
 
id : ID | STRING | HTML_STRING | NUMBER
   ;
 
/** "a numeral [-]?(.[0-9]+ | [0-9]+(.[0-9]*)? )" */ NUMBER
   : '-'? ( '.' DIGIT+ | DIGIT+ ( '.' DIGIT* )? )
   ;
 
 
fragment DIGIT
   : [0-9]
   ;
 
/** "any double-quoted string ("...") possibly containing escaped quotes" */ STRING
   : '"' ( '\\"' | . )*? '"'
   ;
 
/** "Any string of alphabetic ([a-zA-Z\200-\377]) characters, underscores
 *  ('_') or digits ([0-9]), not beginning with a digit"
 */ ID
   : LETTER ( LETTER | DIGIT )*
   ;
/*
 ID_WITH_SPACE
   : LETTER ( ' ' | LETTER | DIGIT )*
   ;
*/
 
fragment LETTER
   : [a-zA-Z\u0080-\u00FF_]
   ;
 
/** "HTML strings, angle brackets must occur in matched pairs, and
 *  unescaped newlines are allowed."
 */ HTML_STRING
   : '<' ( TAG | ~ [<>] )* '>'
   ;
 
fragment TAG
   : '<' .*? '>'
   ;
 
COMMENT
   : '/*' .*? '*/' -> skip
   ;
 
LINE_COMMENT
   : '//' .*? '\r'? '\n' -> skip
   ;
 
/** "a '#' character is considered a line output from a C preprocessor (e.g.,
 *  # 34 to indicate line 34 ) and discarded"
 */ PREPROC
   : '#' .*? '\n' -> skip
   ;
 
WS
   : [ \t\n\r]+ -> skip
   ;
 
 
BlockComment
    :   '/*' .*? '*/'
        -> skip
    ;
 
LineComment
    :   '//' ~[\r\n]*
        -> skip
    ;
fsm2.dot
digraph finite_state_machine {
	 rankdir=LR;
	size="10" 
 
//////////////////////////////////////////////////////////////////////////////////////////////////////
	1 [shape=doublecircle];
	//définit l'action sur état
	  a1 [shape=box,label=  <
 	<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
	 <TR><TD>I</TD><TD>sortie4</TD> </TR>  
	 </TABLE>>  ];
	//attache l'action sur état
	1 ->a1  [arrowhead=none]     ;
//////////////////////////////////////////////////////////////////////////////////////////////////////
	2 [shape = circle];
////////////////////////////////////////////////////////////////////////////////////////////////////// 
	3 [shape = circle];
	 //définit l'action sur état 
	  a3[shape=box,label=  <
	<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4">
	 <TR><TD>I</TD><TD>sortie3</TD> </TR> 
	 <TR><TD>R</TD><TD>sortie4=IN</TD> </TR>
	 </TABLE>>  ];
	//attache l'action sur état
	3 ->a3[arrowhead=none];
//////////////////////////////////////////////////////////////////////////////////////////////////////  
//état vide pour origine reset synchrone
	node [shape=box]
	SRESET1 [shape=box, style="invis" ] 
	SRESET2 [shape=box, style="invis" ]
	SRESET1->2  [style="dashed", shape=box, label=  <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR>   <TD COLSPAN="1">SRESET AND IN</TD> </TR> </TABLE>>  ];
	SRESET2->3 [style="dashed", shape=box, label=  <
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR>   <TD COLSPAN="2">SRESET AND NOT IN</TD> </TR> <TR> <TD>I</TD><TD>sortie2</TD> </TR> 
	<TR> <TD>R</TD><TD>sortie5=entree2</TD> </TR> 
	</TABLE>>  ];
 
 
 
////////////////////////////////////////////////////////////////////////////////////////////////////// 
//Actions sur transitions
	1 -> 2 [label=  <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR>   <TD COLSPAN="1">NOT IN</TD> </TR> </TABLE>>  ];
 	1 -> 3 [label=  <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR>   <TD COLSPAN="1">IN</TD> </TR> </TABLE>>  ]; 
	3 -> 2 [label=  <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR>   <TD COLSPAN="1">NOT IN</TD> </TR> </TABLE>>  ];
 	2 -> 3 [label=  <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR>   <TD COLSPAN="1">IN</TD> </TR> </TABLE>>  ]; 
	3 -> 1[label=  <
	<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="4"> <TR>   <TD COLSPAN="2">IN</TD> </TR> <TR> <TD>I</TD><TD>sortie1</TD> </TR> 
	<TR> <TD>S</TD><TD>sortie2</TD> </TR> 
	</TABLE>>  ];
}

Affichage par module Graphviz du dokuwiki

Autres

antlr

http://www.antlr.org/

quick start linux:

sudo su-
cd /usr/local/lib
wget http://www.antlr.org/download/antlr-4.5.1-complete.jar
exit
echo "export CLASSPATH=".:/usr/local/lib/antlr-4.5.1-complete.jar:$CLASSPATH"" >>~/.bashrc
echo "alias antlr4='java -jar /usr/local/lib/antlr-4.5.1-complete.jar'"  >>~/.bashrc
echo "alias grun='java org.antlr.v4.gui.TestRig'"  >>~/.bashrc

video: http://www.youtube.com/watch?v=q8p1voEiu8Q&feature=youtu.be

exemple d'utilisation : http://www.youtube.com/watch?v=Bhnw_vexdAI&list=PLCB70903D812AD11A

http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4

il faut java 1.6: http://support.apple.com/kb/dl1359?locale=en_US

choix de la version de java: http://superuser.com/questions/490425/how-do-i-switch-between-java-7-and-java-6-on-os-x-10-8-2

/usr/libexec/java_home -v 1.6.0_26 --exec javac -version

pour savoir ou est installé la version 1.6 de java

/usr/libexec/java_home -v 1.6.0_26

pour lancer java 1.6

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -version

du coup les alias à créer pour antlr:

export CLASSPATH=".:/usr/local/lib/antlr-4.0-complete.jar:$CLASSPATH"
alias antlr4='/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /usr/local/lib/antlr-4.1-complete.jar'
alias grun='/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java org.antlr.v4.runtime.misc.TestRig'
cd /tmp
vim Hello.g4
    // Define a grammar called Hello
    grammar Hello;
    r  : 'hello' ID ;         // match keyword hello followed by an identifier
    ID : [a-z]+ ;             // match lower-case identifiers
    WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines  
antlr4 Hello.g4
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/javac Hello*.java

grammaire antlr4 pour C: https://github.com/antlr/grammars-v4/commit/15df18159653f7ae2963cc73bec7d2b8939a9363

grammaire antlr3 pour vhdl: http://www.jguru.com/forums/view.jsp?EID=1382922

http://www.antlr3.org/grammar/list.html

http://www.antlr3.org/grammar/1086696923011/vhdlams/

http://www.antlr3.org/grammar/1086696923011/vhdlams/vams.g

parsing vhdl is very hard: http://eli.thegreenplace.net/2009/05/19/parsing-vhdl-is-very-hard/

string template

Cours sur les compilateurs

states.1452076846.txt.gz · Dernière modification : 2016/01/06 11:40 de bvandepo